Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Command with Click Event Handler

Tags:

command

wpf

When I use the Command in a Buttoncontrol the event handler which joined with Click event will never raised,

How can I use the Command and handle the Click event handler?

like image 945
Tomas1 Avatar asked Jun 07 '10 15:06

Tomas1


1 Answers

You could attach the ICommand to another property and execute it from the Click handler.

<Button x:Name="MyButton" Tag="{x:Static ApplicationCommands.Stop}" Click="MyButton_Click" />

and in the handler:

private void MyButton_Click(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    if (button != null)
    {
        var command = button.Tag as ICommand;
        if (command != null)
            command.Execute(button.CommandParameter);
    }
}

You'd also need to do some extra work if you wanted to keep the Command disabling behavior.

like image 56
John Bowen Avatar answered Oct 04 '22 21:10

John Bowen