wpf listview右键问题
所以我已将上下文菜单(右键单击菜单)附加到 wpf 列表视图。
不幸的是,当您右键单击时,它会弹出菜单并选择您所在的任何项目。 有没有办法关闭这种右键单击选择行为,同时仍然允许上下文菜单?
so I have attached a context menu (right-click menu) to a wpf listview.
unfortunately, when you right-click it brings up both the menu and selects whatever item you are over. Is there a way to shut off this right-click select behavior while still allowing the context menu?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关键是在正确的位置设置 PreviewMouseRightButtonDown 事件。 您会注意到,即使没有 ContextMenu,右键单击 ListViewItem 也会选择该项目,因此我们需要在每个项目上设置事件,而不是在 ListView 上。
由于预览事件是隧道,这将阻止 RightMouseButtonDown发生在 ListViewItems 上,阻止它们被选择,但不会阻止 ListView 上的 RightMouseButtonDown,因此仍然允许打开 ContextMenu。
The key is setting the PreviewMouseRightButtonDown event in the correct place. As you'll notice, even without a ContextMenu right clicking on a ListViewItem will select that item, and so we need to set the event on each item, not on the ListView.
Since the preview events are tunneling this will block the RightMouseButtonDown from occurring on the ListViewItems preventing them from being selected, but not prevent the RightMouseButtonDown on the ListView and so still allow the ContextMenu to open.