如何在 RoutedCommand 的已执行回调中检索 CommandTarget ? 感谢。
编辑:添加详细示例
命令类:
static class Commands
{
public static readonly RoutedCommand MyCommand = new RoutedCommand();
}
XAML代码
<Window.CommandBindings>
<CommandBinding Command="{x:Static BasicWpfCommanding:Commands.MyCommand}"
CanExecute="MyCommandCanExecute"
Executed="MyCommandExecuted"/>
</Window.CommandBindings>
<StackPanel>
<Button Command="{x:Static BasicWpfCommanding:Commands.MyCommand}"
CommandParameter="#FF303030"
CommandTarget="{Binding ElementName=aButton}"
Name="aButton">A Command</Button>
</StackPanel>
命令回调
private void MyCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void MyCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
//var target = (Button)sender fires an ecception: in effect "sender" is the main window...
}
答案 0 :(得分:1)
var target = e.Source;