Reputation: 1357
I use standard Cut, Copy and Paste commands (which is part of ApplicationCommands class). Is it possible to redefine CanExecute method?
Here is my code:
XAML:
<Window.CommandBindings>
<CommandBinding Command="Copy"
CanExecute="CopyCanExecute" Executed="CopyExecuted"/>
</Window.CommandBindings>
<StackPanel>
<TextBox Name="txt"></TextBox>
<Button Command="Copy" CommandTarget="{Binding ElementName=txt}">copy</Button>
</StackPanel>
Code-behind:
private void CopyCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}
private void CopyExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Copy Executed");
}
The button still behave like its command is standard Copy command.
Upvotes: 3
Views: 3606
Reputation: 71
You can set the commandbinding to the textbox directly instead of to the window.
Upvotes: 0
Reputation: 21
The copy command will not work when the focus is on a textbox where the commands have already been handled, but it will work on elements like CheckBox etc.
Upvotes: 1
Reputation: 50038
In the CanExecute handler you might need to add `e.Handled = true; also, so that it doesnt go and execute the standard Copy.CanExecute()
Upvotes: 0
Reputation: 564413
You do this via a CommandBinding. The local CommandBinding can specify a CanExecuteHandler.
For details and a working example, see this blog post.
Upvotes: 1