【问题标题】:WPF Command with Click Event Handler带有 Click 事件处理程序的 WPF 命令
【发布时间】:2010-06-07 15:30:14
【问题描述】:

当我在Buttoncontrol 中使用Command 时,与Click 事件结合的事件处理程序永远不会引发,

如何使用Command 和处理Click 事件处理程序?

【问题讨论】:

    标签: wpf command


    【解决方案1】:

    您可以将 ICommand 附加到另一个属性并从 Click 处理程序执行它。

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

    在处理程序中:

    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);
        }
    }
    

    如果您想保持命令禁用行为,您还需要做一些额外的工作。

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        命令是一种周事件。关于命令的好处是您不需要使用事件。实际上,如果您使用 Viewmodel 模式,则根本不需要使用代码和事件。见中继命令:http://blog.galasoft.ch/archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx

        【讨论】:

        • 不对,如果您希望查看更新 ViewModel 中的 SeletedItem 等内容或以特定方式处理选择的工作方式,则需要使用后面的代码。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多