如何在 wpf 中使用 RelayCommand?

发布于 2024-07-19 09:11:07 字数 45 浏览 6 评论 0 原文

如何在 wpf 中使用 RelayCommand

How can I use the RelayCommand in wpf?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2024-07-26 09:11:07

中继命令在 WPF 中不存在,它只是一个外部类,在 这篇 MSDN 文章。 如果你想使用它,你需要自己编写它。

否则,您可以从 WPF 工具包此处使用 Delegate 命令,其中与 RelayCommand 代码相比,有一些额外的功能。


啊,当我输入这个答案时,问题发生了变化。 假设您使用上面定义的 RelayCommand,您需要为其提供一个或两个委托,一个返回一个布尔值,确定该命令是否处于要运行的有效状态,另一个不返回任何内容并实际运行命令。 如果您不提供“CanRun”委托,则该命令将认为它始终处于有效状态。 本文中使用的代码:

RelayCommand _saveCommand;
public ICommand SaveCommand
{
    get
    {
        if (_saveCommand == null)
        {
            _saveCommand = new RelayCommand(param => this.Save(),
                param => this.CanSave );
        }
        return _saveCommand;
    }
}

声明一个 RelayCommand,该命令将在触发时调用 Save() 方法并返回 CanSave 属性作为有效性测试。 当此命令绑定到 WPF 中的按钮时,按钮的 IsEnabled 属性将与 ViewModel 的 CanSave 属性匹配,并且当单击按钮(假设已启用)时,将在 ViewModel 上调用 Save() 方法。

Relay command doesn't exist in WPF, it is just a external class that raised to prominence after it was defined in this MSDN article. You need to write it yourself if you want to use it.

Otherwise you can you the Delegate command from the WPF toolkit here which has a little bit of extra functionality over the RelayCommand code.


Ah, the question changed while I was typing this answer. Assuming that you are using the RelayCommand as defined above you need to supply it with one or two delegates, one that returns a bool which determines whether the command is in a valid state to be run, and a second which returns nothing and actually runs the command. If you don't supply a "CanRun" delegate then the command will consider that it is always in a valid state. The code used in the article:

RelayCommand _saveCommand;
public ICommand SaveCommand
{
    get
    {
        if (_saveCommand == null)
        {
            _saveCommand = new RelayCommand(param => this.Save(),
                param => this.CanSave );
        }
        return _saveCommand;
    }
}

Declares a RelayCommand that will call the Save() method when triggered and return the CanSave property as a test for validity. When this command is bound to a button in WPF the IsEnabled property of the Button will match the CanSave property of the ViewModel and when the button is clicked (assuming it is enabled) the Save() method will be called on the ViewModel.

另类 2024-07-26 09:11:07

作为为所有方法创建 RelayCommand 包装器的替代方案,我可以建议一个免费的库和源代码,它允许您使用绑定 {BindTo Save()}。 我创建它是为了简化我的绑定。 它还使相对绑定变得更加容易。 您可以在这里找到它: http://www.simplygoodcode.com/2012 /08/simler-wpf-binding.html

As an alternative to creating RelayCommand wrappers for all your methods can I suggest a free library and source that will allow you to use the binding {BindTo Save()}. I created it to simplify my bindings. It also makes relative binding much easier. You can find it here: http://www.simplygoodcode.com/2012/08/simpler-wpf-binding.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文