如何根据右键单击的 WPF DataGrid 行添加上下文菜单?
我需要在 ContextMenu 中显示不同的选项,具体取决于右键单击 WPF DataGrid 的哪一行。我最初的想法是通过绑定或处理鼠标单击事件来实现此目的,但到目前为止,这两种策略都没有取得成功。任何帮助将不胜感激!
谢谢你!
丹妮丝
I need to display different options in a ContextMenu depending on which row of a WPF DataGrid is right-clicked. My initial ideas were to accomplish this through either binding or handling a mouse click event, but I haven't had success with either strategy so far. Any help would be most appreciated!
Thank you!
Denise
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以处理 DataGrid 的 ContextMenuOpening 事件,并根据路由事件的原始来源调整上下文菜单。
下面是一个示例,如果原始源的数据上下文是 Inventory 类型,我将显示上下文菜单,否则我不会通过处理事件来显示上下文菜单。
我确信现在为您提供帮助为时已晚,但以防万一对于遇到此问题的其他人来说还不算太晚。
You can handle the DataGrid's ContextMenuOpening event and based on the original source of the routed event you adjust your context menu.
Below is a sample where I show a context menu if the data context of the original source is of type Inventory otherwise I do not show the context menu by handling the event.
I'm sure it is too late to help you now, but in case it is not too late and for anyone else who comes across this.
您可以尝试 ContextMenuOpening 事件中 ContextMenuEventArgs 参数中的 OriginalSource :
但请注意,TemplatedParent 的使用取决于数据网格项的绑定方式
You can try the OriginalSource from the ContextMenuEventArgs argument in the ContextMenuOpening event :
Note however that the use of TemplatedParent depends on how the datagrid items were bound