将控件的"已启用"属性绑定到变量

use*_*746 2 c# winforms

我遇到了一些我在一些类似帖子上发现的问题,然而,它们并不完全相同,我不太确定如何将它应用到我的场景中.它们可能与我的情况相同或不同.所以,我希望在这里发布我自己的问题,我将得到我的具体情况的答案.

基本上,我有一个带有一堆控件的窗口表单.我希望能够将其Enabled属性绑定到我设置的布尔变量,以便可以根据我的判断启用或禁用它们.

public partial class MyUI : Form
{
    private int _myID;
    public  int  myID 
    {
        get
        {
            return _myID;;
        }
        set
        {
            if (value!=null)
            {
                _bEnable = true;
            }
        }
    }
    private bool _bEnable = false;
    public bool isEnabled
    {
        get { return _bEnable; }
        set { _bEnable = value; }
    }

    public myUI()
    {
        InitializeComponent();
    }

public void EnableControls()
{
   if (_bEnable)
   {
    ctl1.Enabled = true;
    ctl2.Enabled = true;
            ......
    ctl5.Enabled = true;
   }
       else
   {
    ctl1.Enabled = false;
    ctl2.Enabled = false;
            ......
    ctl5.Enabled = false;
       }
}
}
Run Code Online (Sandbox Code Playgroud)

}

上面的EnableControls方法可以满足我的需要,但它可能不是最好的方法.我更喜欢将ctrl1..5绑定到我的变量_bEnable.变量将根据用户输入的一个字段而改变,如果字段中的值存在于数据库中,则将启用其他控件以供用户更新,否则将禁用它们.

我在这里找到了一个非常相似的问题,但数据绑定到文本字段.如何摆脱EnableControls方法并将_bEnabled的值绑定到每个控件中的"Enabled"属性?

Tod*_*les 9

去研究MVVM(Model - View - ViewModel)模式,特别是它在Windows Forms中的实现.将它应用到WPF/Silverlight应用程序要容易得多,但是你仍然可以在Windows Forms中使用它而不会有太多麻烦.

要直接解决您的问题,您需要做两件事:

  1. 创建一个将保持内部状态的类(即是否启用了按钮).这个类必须实现INotifyPropertyChanged.这将是MVVM模式中的View Model.
  2. 将上面1.)中的类的实例绑定到您的表单.您的表单是MVVM模式中的View.

完成上面的1和2之后,您可以更改类的状态(即更改表示按钮是否从true启用为false的属性),并且将自动更新表单以显示此更改.

下面的代码应足以使概念有效.你需要明显扩展它,但它应该足以让你开始.

查看模型

public class ViewModel : INotifyPropertyChanged
{
    private bool _isDoStuffButtonEnabled;
    public bool IsDoStuffButtonEnabled
    {
        get
        {
            return _isDoStuffButtonEnabled;
        }
        set
        {
            if (_isDoStuffButtonEnabled == value) return;
            _isDoStuffButtonEnabled = value;
            RaisePropertyChanged("IsDoStuffButtonEnabled");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

视图

public class View : Form
{
    public Button DoStuffButton { get; set; }

    public void Bind(ViewModel vm)
    {
        DoStuffButton.DataBindings.Add("Enabled", vm, "IsDoStuffButtonEnabled");
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

public class Startup
{
    public ViewModel ViewModel { get; set; }
    public View View { get; set; }

    public void Startup()
    {
        ViewModel = new ViewModel();
        View = new View();

        View.Bind(ViewModel);

        View.Show();

        ViewModel.IsDoStuffButtonEnabled = true;

        // Button becomes enabled on form.

        // ... other stuff here.
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 根据您绑定到的Windows窗体控件的哪个属性,可能会进行双向绑定.双向绑定是两端的更改都被传达(就像文本框值被输入到它所绑定的视图模型的属性中一样).您是否阅读过[Windows窗体的MVVM模式](http://www.codeproject.com/Articles/364485/MVVM-Model-View-ViewModel-Patte)?它从标准的Windows Forms开发转变为一种范式,但由于各种原因它的效果要好得多. (2认同)