在执行阶段检索CommandTarget

时间:2010-07-09 14:47:43

标签: wpf

如何在 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...
}

1 个答案:

答案 0 :(得分:1)

var target = e.Source;