开发者

WPF Command with Click Event Handler

开发者 https://www.devze.com 2023-01-02 08:41 出处:网络
When I use the Command in a Buttoncontrol the event handler which joined with Click event will 开发者_StackOverflow中文版never raised,

When I use the Command in a Buttoncontrol the event handler which joined with Click event will 开发者_StackOverflow中文版never raised,

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


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.


You need the EventToCommand behaviour in MVVMLight


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

0

精彩评论

暂无评论...
验证码 换一张
取 消