WPF context menu Datagrid Row - disable menu item

Hi, I am starting a programmer and new to WPF, and I have a simple question, but I spent quite a bit of time looking for it, and I could not figure it out, so I hope you guys help me. All I want is to disable the menu item in the context menu in my Datagrid. for example: if several rows are selected in Datagrid, disable the "Properties" context menu item

<DataGrid.Resources> <ContextMenu x:Key="DataRowContextMenu"> <MenuItem x:Name="RowContMenuProp" Header="Properties"> <MenuItem.Icon> <Image Source="Resources/proporties.ico" Height="16" Width="16" /> </MenuItem.Icon> </MenuItem> <Separator Margin="0" /> <MenuItem Header="Copy" Command="Copy" > <MenuItem.Icon> <Image Source="Resources/copy.ico" Height="16" Width="16" /> </MenuItem.Icon> </MenuItem> <MenuItem Header="Remove from list" Click="MenuItem_Click_1" > <MenuItem.Icon> <Image Source="Resources/cut.png" Height="16" Width="16" /> </MenuItem.Icon> </MenuItem> <MenuItem Header="Remove from project" Click="MenuItem_Click_2" > <MenuItem.Icon> <Image Source="Resources/remove.ico" Height="16" Width="16" /> </MenuItem.Icon> </MenuItem> </ContextMenu> </DataGrid.Resources> <DataGrid.RowStyle > <Style TargetType="{x:Type DataGridRow}"> <Setter Property="ContextMenu" Value="{StaticResource DataRowContextMenu}" /> <Setter Property="BorderThickness" Value="0"/> </Style> </DataGrid.RowStyle> 

- disable the context menu item

 Private Sub datagrid1_MouseUp(sender As Object, e As MouseButtonEventArgs) If datagrid1.SelectedItems.Count > 1 Then 
+5
source share
2 answers

This is one way around this. It is a bit dirty, but it works.

First create an IValueConverter to say that 1 means enable (true), and Value Converter might look like this:

 public class OneReturnsTrueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (int)value == 1; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } 

Then save the link to the DataGrid in the DataGridRow

 <DataGrid.RowStyle > <Style TargetType="{x:Type DataGridRow}"> <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" /> <Setter Property="ContextMenu" Value="{StaticResource DataRowContextMenu}" /> <Setter Property="BorderThickness" Value="0"/> </Style> </DataGrid.RowStyle> 

And finally, bind the SelectedItems value of the grid to the IsEnabled property

 <DataGrid.Resources> <myConverters:OneReturnsTrueConverter x:Key="OneReturnsTrueConverter"/> <ContextMenu x:Key="DataRowContextMenu"> <MenuItem x:Name="RowContMenuProp" Header="Properties" DataContext="{Binding Parent.PlacementTarget.Tag , RelativeSource={RelativeSource Self}}" IsEnabled="{Binding Path=SelectedItems.Count, Converter={StaticResource OneReturnsTrueConverter}}" /> </ContextMenu> </DataGrid.Resources> 
+2
source

Take a look at the WPF command template implementation . The command can indicate whether an action is possible by implementing the CanExecute method. The button can subscribe to the CanExecuteChanged event and be disabled if CanExecute returns false or enabled if CanExecute returns true .

You can easily adapt the code from the MSDN page to your needs.

 <MenuItem x:Name="RowContMenuProp" Header="Properties" Command="local:ApplicationsCmd.ShowProperties" CanExecute="ShowPropertiesCanExecute" Executed="ShowPropertiesExecuted" > 

Then in the code:

 Private Sub ShowPropertiesExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs) ... End Sub Private Sub ShowPropertiesCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs) e.CanExecute = ...set to True when your condition is met End Sub 
+1
source

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


All Articles