tags:

views:

151

answers:

4

Hi all,

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

how can I use the Command and handle the Click event handler

Thanks in advance

A: 

You can't..

Use either Command or Click

or if you really have to hack your way through with a PreviewMouseLeftButtonDown event ;)

Arcturus
A: 

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.

John Bowen
I used it, thanks john
Tomas1
A: 

A command is a kind of week event. The good thinks about commands is that you do not need to use events. Actually you do not need to use code behind at all and events if you use a Viewmodel pattern. See relay commands: http://blog.galasoft.ch/archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx

Sirius
A: 

You need the EventToCommand behaviour in MVVMLight

Veer