views:

15

answers:

1

How can I retrieve the CommandTarget in the Executed callback of a RoutedCommand? Thanks.

Edit: adding a verbose sample

Commands class:

static class Commands
{
   public static readonly RoutedCommand MyCommand = new RoutedCommand();
} 

XAML code

<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>

Commands callbacks

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...
}
A: 
var target = e.Source;
John Bowen