带有 Click 事件处理程序的 WPF 命令 [英] WPF Command with Click Event Handler

查看:18
本文介绍了带有 Click 事件处理程序的 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?

解决方案

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.

这篇关于带有 Click 事件处理程序的 WPF 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆