绑定到 WinForms 中的命令 [英] Binding to commands in WinForms

查看:32
本文介绍了绑定到 WinForms 中的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在带有 MVVM 的 WPF 等视图模型中将按钮绑定到命令?

How can a button be bound to a command in a view model like in WPF with MVVM?

推荐答案

我已将 ICommand 对象附加到 ButtonTag 属性和之前的 MenuItem 对象.

I've attached ICommand objects to the Tag property of Button and MenuItem objects before.

然后,我只是看看我是否可以投射并运行它,例如:

Then, I just see if I can cast and run it if I can, example:

private void button1_Click(object sender, EventArgs e)
{
    ICommand command = ((Control)(sender)).Tag as ICommand;

    if (command != null)
    {
        command.Execute();
    }
}

为了更轻松的生活,请尝试子类化控件(例如 ButtonMenuItem)

For even an easier life, try subclassing the controls (e.g. Button, MenuItem)

这篇关于绑定到 WinForms 中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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