如何根据右键单击的 WPF DataGrid 行添加上下文菜单?

发布于 2024-08-07 20:23:26 字数 143 浏览 3 评论 0原文

我需要在 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 技术交流群。

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

发布评论

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

评论(2

∞梦里开花 2024-08-14 20:23:26

您可以处理 DataGrid 的 ContextMenuOpening 事件,并根据路由事件的原始来源调整上下文菜单。

下面是一个示例,如果原始源的数据上下文是 Inventory 类型,我将显示上下文菜单,否则我不会通过处理事件来显示上下文菜单。

Private Sub InventoriesDataGrid_ContextMenuOpening( _
    ByVal sender As Object, _
    ByVal e As System.Windows.Controls.ContextMenuEventArgs) Handles _
    InventoriesDataGrid.ContextMenuOpening

    Dim context = DirectCast(e.OriginalSource, System.Windows.FrameworkElement).DataContext

    If TypeOf context Is Inventory Then
        InventoriesDataGrid.ContextMenu = InventoriesDataGrid.Resources("DefaultContextMenu")
    Else
        e.Handled = True 'Do not show context menu.
    End If
End Sub

我确信现在为您提供帮助为时已晚,但以防万一对于遇到此问题的其他人来说还不算太晚。

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.

Private Sub InventoriesDataGrid_ContextMenuOpening( _
    ByVal sender As Object, _
    ByVal e As System.Windows.Controls.ContextMenuEventArgs) Handles _
    InventoriesDataGrid.ContextMenuOpening

    Dim context = DirectCast(e.OriginalSource, System.Windows.FrameworkElement).DataContext

    If TypeOf context Is Inventory Then
        InventoriesDataGrid.ContextMenu = InventoriesDataGrid.Resources("DefaultContextMenu")
    Else
        e.Handled = True 'Do not show context menu.
    End If
End Sub

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.

記憶穿過時間隧道 2024-08-14 20:23:26

您可以尝试 ContextMenuOpening 事件中 ContextMenuEventArgs 参数中的 OriginalSource :

DataGridResults.ContextMenuOpening += (sender, args) =>
{
    var frameworkElement = args.OriginalSource as FrameworkElement;
    var gridRow = frameworkElement != null ? frameworkElement.TemplatedParent as DataGridRow : null;
}

但请注意,TemplatedParent 的使用取决于数据网格项的绑定方式

You can try the OriginalSource from the ContextMenuEventArgs argument in the ContextMenuOpening event :

DataGridResults.ContextMenuOpening += (sender, args) =>
{
    var frameworkElement = args.OriginalSource as FrameworkElement;
    var gridRow = frameworkElement != null ? frameworkElement.TemplatedParent as DataGridRow : null;
}

Note however that the use of TemplatedParent depends on how the datagrid items were bound

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