I have a DataGrid
and I do not know, why the MenuItems
of ContextMenu
are 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>
What can be the cause for that? I did not find any code, which is responsible for setting the ICommand.CanExecute
or the MenuItem.IsEnabled
.
Please tell me which information I still need to provide.
@Maverik: I do not wrote any code for those three standard .NET commands:
Your MenuItems are built-in WPF commands. Accordingly to MSDN documentation their implementation depends on control where commands were triggered and in your case from the state of DataGrid(row selected or not etc.).
...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.
See ApplicationCommands Class.
You can impact your ContextMenu by putting in XAML:
<DataGrid.CommandBindings>
<CommandBinding Command="Copy" CanExecute="CommandBinding_CanExecute"/>
</DataGrid.CommandBindings>
and in code behind:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;//put here your logic
e.Handled = true;
}