WPF Datagrid 行上下文菜单 - 禁用菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25631466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid Row Context Menu - Disable Menu Item
提问by Dude
Hi There I am beginner programmer and new to WPF and have a simple question, but I have spent quite some time searching it a could not figure it out, so I hope you guys will help me. All I want is to disable menu item in a context menu in my Datagrid. for example: if more than one rows selected in Datagrid , disable a context menu item "Properties"
嗨,我是初学者程序员和 WPF 新手,有一个简单的问题,但我花了很长时间搜索它却无法弄清楚,所以我希望你们能帮助我。我想要的只是在我的 Datagrid 的上下文菜单中禁用菜单项。例如:如果在 Datagrid 中选择了不止一行,则禁用上下文菜单项“属性”
<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 Context menu item
-- 禁用上下文菜单项
Private Sub datagrid1_MouseUp(sender As Object, e As MouseButtonEventArgs)
If datagrid1.SelectedItems.Count > 1 Then
采纳答案by dharshana jagoda
This is one way to go about it. It is a bit dirty but works.
这是一种方法。它有点脏,但有效。
First create an IValueConverter to say that 1 means enable (true) and the Value Converter may look like this
首先创建一个 IValueConverter 表示 1 表示启用(真),值转换器可能看起来像这样
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 a reference to the DataGrid in your DataGridRow
然后在 DataGridRow 中保存对 DataGrid 的引用
<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 count of the grid to the IsEnabled property
最后将网格的 SelectedItems 计数绑定到 IsEnabled 属性
<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>
回答by Patrice Gahide
Take a look at the WPF Command pattern implementation. A command can indicate whether an action is possible by implementing the CanExecute method. A button can subscribe to the CanExecuteChanged event and be disabled if CanExecute returns false or be enabled if CanExecute returns true.
看一看WPF 命令模式实现。命令可以通过实现 CanExecute 方法来指示操作是否可行。按钮可以订阅 CanExecuteChanged 事件并在 CanExecute 返回 false 时禁用或在 CanExecute 返回 true 时启用。
You can easily adapt the code from the MSDN page to your needs.
您可以轻松地根据您的需要调整 MSDN 页面中的代码。
<MenuItem x:Name="RowContMenuProp" Header="Properties"
Command="local:ApplicationsCmd.ShowProperties"
CanExecute="ShowPropertiesCanExecute"
Executed="ShowPropertiesExecuted" >
Then in 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

