Reach "command-source" xaml element from ICommand

231 Views Asked by At

I want to implement ICommand in a separate class, and want to access the button from here:

<Grid>
    <Button Command="{StaticResource Do}" >Do!</Button>
</Grid>

public class DoCommand : ICommand
{
    public void Execute(object parameter)
    {
        **I need to access a xaml that triggered this command from here**
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

How could I do that? Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

You can pass the button as parameter from XAML like this:

<Button Command="{StaticResource Do}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, BindsDirectlyToSource=True}" >Do!</Button>

Then in Execute method:

public void Execute(object parameter) { Button button = parameter as Button; }