我使用标准的剪切、复制和粘贴命令(它是 ApplicationCommands 类的一部分)。是否可以重新定义 CanExecute 方法?
这是我的代码:
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>
代码隐藏:
private void CopyCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}
private void CopyExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Copy Executed");
}
按钮的行为仍然像它的命令是标准复制命令一样。
最佳答案
您可以通过 CommandBinding 执行此操作.本地 CommandBinding 可以指定一个 CanExecuteHandler。
有关详细信息和工作示例,请参阅 this blog post .
关于c# - WPF:如何重新定义 ApplicationCommands 的 CanExecute 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2530480/