WPF Command with Click Event Handler

前端 未结 3 1852
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 11:05

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

How can I use the

相关标签:
3条回答
  • 2021-01-02 11:12

    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.

    0 讨论(0)
  • 2021-01-02 11:13

    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 讨论(0)
  • 2021-01-02 11:21

    You need the EventToCommand behaviour in MVVMLight

    0 讨论(0)
提交回复
热议问题