WPF 应用程序有 2 种模式:基本模式和高级模式。
有一个
DataGrid
,用户可以使用上下文菜单 Rerun
或 Delete
当前项目。Visibility
和 IsEnabled
绑定到 IsAdvancedMode
属性):
<DataGrid.ContextMenu>
<ContextMenu Visibility="{Binding DataContext.IsAdvancedMode, Source={StaticResource ProxyElement}, Mode=OneWay,
Converter={conv:BoolToVisibilityConverter VisibilityForFalse=Hidden}}"
IsEnabled="{Binding DataContext.IsAdvancedMode, Source={StaticResource ProxyElement}, Mode=OneWay}">
.....
</DataGrid.ContextMenu>
当然,只有当用户右键单击
DataGrid
行时才会显示上下文菜单。让我们跳到
如何克服这种影响?
如果我理解你的代码,你可能想要挂钩
ContextMenuOpening
的 DataGrid
事件,并根据 Handled
的值将其标记为 IsAdvancedMode
或不标记它。标记它 Handled
将阻止显示上下文菜单。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
new MainWindowDataContext DataContext =>(MainWindowDataContext)base.DataContext;
private void ContextMenu_Opening(object sender, ContextMenuEventArgs e)
{
e.Handled = !DataContext.IsAdvancedMode;
}
}
<Window x:Class="wpf_context_menu.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpf_context_menu"
xmlns:conv="clr-namespace:wpf_context_menu.Converters"
mc:Ignorable="d"
Title="MainWindow" Width="500" Height="300">
<Window.Resources>
<local:MenuDataContext x:Key="ProxyElement" />
</Window.Resources>
<Window.DataContext>
<local:MainWindowDataContext />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<DataGrid
x:Name="dataGrid"
ItemsSource="{Binding JobItems}"
ContextMenuOpening="ContextMenu_Opening"
AutoGenerateColumns="False"
FontSize="16">
<DataGrid.ContextMenu>
<ContextMenu
ItemsSource="{Binding MenuItems, Source={StaticResource ProxyElement}}" />
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTextColumn Header="Job Name" Binding="{Binding JobName}" Width="*" />
<DataGridTextColumn Header="Start Time" Binding="{Binding StartTime}" />
<DataGridTextColumn Header="Status" Binding="{Binding Status}" />
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox IsChecked="{Binding IsAdvancedMode}" />
<TextBlock Text="Is Advanced Mode" Margin="5,0,0,0" VerticalAlignment="Center" />
</StackPanel>
</Grid>
</Window>
partial class MainWindowDataContext : ObservableObject
{
[ObservableProperty]
private bool _isAdvancedMode;
public ObservableCollection<JobItem> JobItems { get; } = new ObservableCollection<JobItem>
{
new JobItem { JobName = "Data Backup", StartTime = DateTime.Now.AddMinutes(-30), Status = Status.SUCCESS },
new JobItem { JobName = "Data Analysis", StartTime = DateTime.Now.AddMinutes(-60), Status = Status.FAILED },
new JobItem { JobName = "Report Generation", StartTime = DateTime.Now.AddMinutes(-120), Status = Status.SUCCESS },
};
}