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

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

问题描述

在WMV和MVVM中,如何将按钮绑定到视图模型中的命令?

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

推荐答案

我之前已经将ICommand对象附加到ButtonMenuItem对象的Tag属性上.

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天全站免登陆