如何在右键单击事件中突出显示列表框的项目?

发布于 2024-09-26 04:23:49 字数 1090 浏览 6 评论 0原文

我不知道我是否是第一个问这个问题的人(我搜索了整个板块),但我从未找到任何答案。正如标题中所述,每当我右键单击列表框中的项目时,我都会尝试突出显示/选择它。

下面是 XAML 代码:

<ListBox Grid.Row="1" x:Name="ContactList" Margin="6" ItemsSource="{Binding ''}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Status_Image}" Margin="0,0,3,0" />
                <StackPanel Orientation="Vertical">
                    <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Name}" FontWeight="Bold" FontSize="13" Foreground="Black" />
                    <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Message}" FontSize="11" Foreground="Gray" />
                </StackPanel>
    <Image Source="{Binding NotifImg}" Margin="8,0,0,0"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我知道如何处理右键单击并在按钮或单个元素上显示上下文菜单,但不知道如何在绑定的列表框中显示上下文菜单。如果您对我应该如何进行有任何建议,请随时告诉我,因为我目前陷入困境。

谢谢你,埃菲斯曼。

I don't know if I'm the first to ask this question(I searched the whole board) but I never found any answers. As said in the title I am trying to highlight/select an item in my Listbox whenever I right click it.

Here is the XAML code:

<ListBox Grid.Row="1" x:Name="ContactList" Margin="6" ItemsSource="{Binding ''}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Status_Image}" Margin="0,0,3,0" />
                <StackPanel Orientation="Vertical">
                    <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Name}" FontWeight="Bold" FontSize="13" Foreground="Black" />
                    <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Message}" FontSize="11" Foreground="Gray" />
                </StackPanel>
    <Image Source="{Binding NotifImg}" Margin="8,0,0,0"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I know how to handle right click and show a Context menu on a button or a single element, but not on a bound Listbox. If you have any advices on how I should proceed please feel free to tell me because I'm currently stuck.

Thank you, Ephismen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃温暖了心ぐ 2024-10-03 04:23:49

好吧,我找到了一种非常简单干净的方法来实现我想做的事情!

下面是 XAML 代码:

<ListBox Grid.Row="1" x:Name="ContactList"ItemsSource="{Binding ''}" MouseRightButtonDown="ContactList_MouseRightButtonDown" MouseRightButtonUp="ContactList_MouseRightButtonUp">
     <ListBox.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal" >
                 <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Name}" FontWeight="Bold" FontSize="13" Foreground="Black" />
                 <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Message}" FontSize="11" Foreground="Gray" />
                 <Image Source="{Binding NotifImg}" Margin="8,0,0,0"/>
             </StackPanel>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

以及背后的代码:

    private void ContactList_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        e.Handled = true;
    }

    private void ContactList_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), (sender as ListBox)).OfType<ListBoxItem>().First().IsSelected = true;
    }    

不要忘记包含“OfType”的 System.Linq。

以弗斯曼。

Alright I found a very simple and clean way of achieving what I wanted to do!

Here is the XAML code:

<ListBox Grid.Row="1" x:Name="ContactList"ItemsSource="{Binding ''}" MouseRightButtonDown="ContactList_MouseRightButtonDown" MouseRightButtonUp="ContactList_MouseRightButtonUp">
     <ListBox.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal" >
                 <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Name}" FontWeight="Bold" FontSize="13" Foreground="Black" />
                 <TextBlock Height="20" HorizontalAlignment="Left" Text="{Binding Message}" FontSize="11" Foreground="Gray" />
                 <Image Source="{Binding NotifImg}" Margin="8,0,0,0"/>
             </StackPanel>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

And the code behind:

    private void ContactList_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        e.Handled = true;
    }

    private void ContactList_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), (sender as ListBox)).OfType<ListBoxItem>().First().IsSelected = true;
    }    

Don't forget to include System.Linq for the 'OfType'.

Ephismen.

双手揣兜 2024-10-03 04:23:49

多选模式解决方案

上面 Ephismen 的解决方案对于多选模式下的 ListBox 无法正常工作(例如,当 Ctrl 按下时,它不会切换项目的选定状态;当 Ctrl 未按下时,它不会取消选择其他项目,...)。

我建议创建一个自定义 ListBoxItem,并使用自定义鼠标右键单击处理程序。在那里,您可以模拟鼠标左键单击,从而获得完全相同的行为:

public class CustomListBoxItem : ListBoxItem
{
    protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
    {
        OnMouseLeftButtonDown(e);
    }
}

您可能还需要为 ItemsSource 绑定创建一个简单的转换器 - 以替换标准的 ListBoxItem ,默认情况下将由您的 CustomListBoxItem 创建:

public class ItemsToCustomListBoxItemsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return null;

        return
            from object item in (IEnumerable) value
            select new CustomListBoxItem
                {
                    Content = item
                };
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException();
    }
}

以下是 ItemsSource 绑定的外观:

<ListBox
    ...
    ItemsSource="{Binding Converter={StaticResource ItemsToCustomListBoxItemsConverter}}"
    ...>

Multiselection mode solution

The solution from Ephismen above does not work correctly for a ListBox in multiselection mode (e.g. it doesn't toggle item's selected state when Ctrl is down, it does not deselect other items when Ctrl is not down, ...).

I would suggest to create a custom ListBoxItem instead, with a custom right mouse click handler. There you can simulate a left mouse button click and thus get exactly the same behavior:

public class CustomListBoxItem : ListBoxItem
{
    protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
    {
        OnMouseLeftButtonDown(e);
    }
}

You may also need to create a simple converter for the ItemsSource binding - to replace the standard ListBoxItem, which would be created by default, by your CustomListBoxItem:

public class ItemsToCustomListBoxItemsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return null;

        return
            from object item in (IEnumerable) value
            select new CustomListBoxItem
                {
                    Content = item
                };
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException();
    }
}

And here's how the ItemsSource binding would look like:

<ListBox
    ...
    ItemsSource="{Binding Converter={StaticResource ItemsToCustomListBoxItemsConverter}}"
    ...>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文