如何在右键单击事件中突出显示列表框的项目?
我不知道我是否是第一个问这个问题的人(我搜索了整个板块),但我从未找到任何答案。正如标题中所述,每当我右键单击列表框中的项目时,我都会尝试突出显示/选择它。
下面是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了一种非常简单干净的方法来实现我想做的事情!
下面是 XAML 代码:
以及背后的代码:
不要忘记包含“OfType”的 System.Linq。
以弗斯曼。
Alright I found a very simple and clean way of achieving what I wanted to do!
Here is the XAML code:
And the code behind:
Don't forget to include System.Linq for the 'OfType'.
Ephismen.
多选模式解决方案
上面 Ephismen 的解决方案对于多选模式下的 ListBox 无法正常工作(例如,当 Ctrl 按下时,它不会切换项目的选定状态;当 Ctrl 未按下时,它不会取消选择其他项目,...)。
我建议创建一个自定义 ListBoxItem,并使用自定义鼠标右键单击处理程序。在那里,您可以模拟鼠标左键单击,从而获得完全相同的行为:
您可能还需要为
ItemsSource
绑定创建一个简单的转换器 - 以替换标准的ListBoxItem
,默认情况下将由您的CustomListBoxItem
创建:以下是 ItemsSource 绑定的外观:
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:
You may also need to create a simple converter for the
ItemsSource
binding - to replace the standardListBoxItem
, which would be created by default, by yourCustomListBoxItem
:And here's how the ItemsSource binding would look like: