您好,我是初学者程序员,也是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>
- 禁用上下文菜单项
Private Sub datagrid1_MouseUp(sender As Object, e As MouseButtonEventArgs)
If datagrid1.SelectedItems.Count > 1 Then
答案 0 :(得分:2)
这是一种解决方法。它有点脏,但有效。
首先创建一个IValueConverter来表示1表示启用(true),而Value Converter可能看起来像这样
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();
}
}
然后在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>
最后将网格的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>
答案 1 :(得分:1)
看看WPF Command pattern implementation。 命令可以通过实现CanExecute方法指示是否可以执行操作。按钮可以订阅CanExecuteChanged事件,如果CanExecute返回false,则被禁用;如果CanExecute返回true,则被启用
您可以轻松地根据需要调整MSDN页面中的代码。
<MenuItem x:Name="RowContMenuProp" Header="Properties"
Command="local:ApplicationsCmd.ShowProperties"
CanExecute="ShowPropertiesCanExecute"
Executed="ShowPropertiesExecuted" >
然后在代码中:
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