DataGrid ContextMenu MenuItems 有时被禁用
DataGrid ContextMenu MenuItems are sometimes disabled
我有一个DataGrid
,我不知道为什么ContextMenu
的MenuItems
是sometimes enabled and sometimes disabled。
<DataGrid ItemsSource="{Binding Values}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
<MenuItem Command="Delete" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
这可能是什么原因造成的?我没有找到任何负责设置ICommand.CanExecute
or the MenuItem.IsEnabled
.
的代码
请告诉我还需要提供哪些信息。
@Maverik:我没有为这三个标准 .NET 命令编写任何代码:
您的菜单项是内置的 WPF 命令。根据 MSDN 文档,它们的实现取决于控制触发命令的位置,在您的情况下来自 DataGrid 的状态(选择或不选择行等)。
...The implementation logic is bound to the command with a
CommandBinding. For example, if the Close command is executed on a
control, the logic which performs the Close command may not be
provided by the control, so the application writer will be responsible
for writing the logic that determines how the control will handle the
command.
Many controls do provide implementation logic for many of the commands
in the command library. For example, the TextBox class provides logic
for the Paste, Cut, Copy, Undo, and Redo commands.
您可以通过输入 XAML 来影响您的上下文菜单:
<DataGrid.CommandBindings>
<CommandBinding Command="Copy" CanExecute="CommandBinding_CanExecute"/>
</DataGrid.CommandBindings>
在后面的代码中:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;//put here your logic
e.Handled = true;
}
我有一个DataGrid
,我不知道为什么ContextMenu
的MenuItems
是sometimes enabled and sometimes disabled。
<DataGrid ItemsSource="{Binding Values}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
<MenuItem Command="Delete" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
这可能是什么原因造成的?我没有找到任何负责设置ICommand.CanExecute
or the MenuItem.IsEnabled
.
请告诉我还需要提供哪些信息。
@Maverik:我没有为这三个标准 .NET 命令编写任何代码:
您的菜单项是内置的 WPF 命令。根据 MSDN 文档,它们的实现取决于控制触发命令的位置,在您的情况下来自 DataGrid 的状态(选择或不选择行等)。
...The implementation logic is bound to the command with a CommandBinding. For example, if the Close command is executed on a control, the logic which performs the Close command may not be provided by the control, so the application writer will be responsible for writing the logic that determines how the control will handle the command.
Many controls do provide implementation logic for many of the commands in the command library. For example, the TextBox class provides logic for the Paste, Cut, Copy, Undo, and Redo commands.
您可以通过输入 XAML 来影响您的上下文菜单:
<DataGrid.CommandBindings>
<CommandBinding Command="Copy" CanExecute="CommandBinding_CanExecute"/>
</DataGrid.CommandBindings>
在后面的代码中:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;//put here your logic
e.Handled = true;
}