[英]WPF C# XAML ListBox mouse events (right click vs left click)
I have a ListBox
dynamically generated. 我有一个动态生成的
ListBox
。
In this ListBox
there are some Items that the user could select, one or more, by left click. 在此
ListBox
框中,用户可以通过单击鼠标左键选择一些或多个项目。
With the right click on an Item of the ListBox
area he could show a context menu. 右键单击
ListBox
区域的Item,他可以显示一个上下文菜单。
My problem is that if the user right-clicks in the ListBox area all goes right, but if he right-clicks on an Items he toggles the selection. 我的问题是,如果用户在ListBox区域中单击鼠标右键,则一切正常,但是如果用户在“项目”上单击鼠标右键,则会切换选择。
I want to avoid the Items toggling by the right click. 我想避免右键单击项切换。
This is how I configured the ListBox
in the MyWindow.cs
: 这就是我在
MyWindow.cs
配置ListBox
的方式:
MyBeautifulList.SelectionMode = SelectionMode.Extended;
And this is a portion of the relative XAML file: 这是相对XAML文件的一部分:
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Send file" Click="SendFileToUser" />
<MenuItem Header="Send folder" Click="SendFolderToUser" />
<MenuItem Header="Copy user ID to the clipboard" Click="copyUserIDtoClipboard" />
</ContextMenu>
</ListBox.ContextMenu>
As well as click, wpf offers a bunch of other options. 除了单击之外,wpf还提供了许多其他选项。 Due to bubbling and tunnelling routed events which you might want to read up on.
由于冒泡和隧道路由事件,您可能需要继续阅读。 You can handle the preview version of an event, mark it as handled and then it stops it firing.
您可以处理事件的预览版本,将其标记为已处理,然后停止触发。
void MenuItem_PreviewRightMouseButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
You will then have no context menu appear, so you will need to show the context menu using code. 这样,您将不会出现任何上下文菜单,因此您将需要使用代码来显示上下文菜单。 Name your context menu
命名上下文菜单
<ContextMenu Name="SomeMeaningfulName">
You can then set IsOpen in code in your new handler, like: 然后可以在新处理程序的代码中设置IsOpen,例如:
SomeMeaningfulName.IsOpen = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.