How to add ContextMenu depending on which row of WPF DataGrid is right clicked?

I need to display various parameters in ContextMenu depending on which row of WPF DataGrid is right clicked. My initial ideas were to accomplish this by binding or handling a mouse click event, but so far I have not had success with any strategy. Any help would be appreciated!

Thanks!

Denise

+3
source share
2 answers

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

, , , , - , .

+5

SourceSource ContextMenuEventArgs ContextMenuOpening:

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

, , TemplatedParent , datagrid

0

Source: https://habr.com/ru/post/1720330/


All Articles