【问题标题】:Binding a bool property to BackColor Property of a WinForm将 bool 属性绑定到 WinForm 的 BackColor 属性
【发布时间】:2013-11-29 19:55:23
【问题描述】:

我的WinForm 应用程序中有一个Form,其中包含一个TextBox,并且此TextBox 绑定到FirstName 对象的FirstName 属性。

public class Person
{
   string firstName;
   public string FirstName
   {
      get { return firstName; }
      set { 
           firstName = value; 
           this.isOdd = value.Length % 2;
          }
   }

   bool isOdd;  
   public bool IsOdd { get {return isOdd; } }
}

当我的应用程序运行时,这个Form 显示并且用户可以在文本框中键入他/她的名字,我如何将FormBackColor 属性绑定到IsOdd 对象的IsOdd 属性(当IsOddTrue BackColor 设置为Color.Green 并且当它是FalseBackColor 设置为Color.Red)?

【问题讨论】:

  • 你能发布一些将name属性绑定到文本框文本的代码吗?

标签: c# winforms data-binding binding


【解决方案1】:

winforms 中的Binding 也有与wpf 非常相似的东西。在WPF 中有Converter,在winforms 中是的,它由一个名为Format 的事件支持。你可以试试这段代码:

Binding bind = new Binding("BackColor", person, "IsOdd");
bind.Format += (s, e) => {
   e.Value = (bool)e.Value ? Color.Green : Color.Red;
};
control.DataBindings.Add(bind);

对于Person 类,你需要稍微修改一下。在winforms 中,有一种通知更改的模式是使用名称为EventNameChanged 的事件以及名称为OnEventNameChanged 的引发者。您可以在winforms 中找到大部分实现此模式。您也可以使用INotifyPropertyChanged,在WPF 中更熟悉。这是修改后的类:

public class Person {
  string firstName;
  public string FirstName {
     get { return firstName; }
     set {
           firstName = value;
           IsOdd = value.Length % 2 != 0;//Note use IsOdd not isOdd
         }
  }
  bool isOdd;
  public bool IsOdd {
    get { return isOdd; }
    private set { 
         if(isOdd != value){
           isOdd = value;
           OnIsOddChanged(EventArgs.Empty);
         }
    }
    public event EventHandler IsOddChanged;
    protected virtual void OnIsOddChanged(EventArgs e) {
      var handler = IsOddChanged;
      if (handler != null) handler(this, e);
    }        
}

注意您可以使用private set 允许所有私有代码通过setter 更改属性IsOdd 并正确通知更改,使用私有变量isOdd 不会通知更改除非您必须在那之后附加一些通知代码。此代码也经过测试!

【讨论】:

    【解决方案2】:

    您不能将Color 属性完全绑定到bool,您必须这样做。 添加 Color 类型的只读属性取决于您的布尔值并绑定它。

    internal class MyClass : INotifyPropertyChanged
    {
        private bool _isOdd;
        public bool IsOdd 
        {
            get
            {
                return _isOdd;
            }
            set
            {
                _isOdd = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsOdd"));
                    PropertyChanged(this, new PropertyChangedEventArgs("Color"));
                }
            }
        }
    
        public Color Color
        {
            get
            {
                return (IsOdd) ? Color.Green : Color.Red;
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    然后只需将该类实例绑定到任何控件的BackColor 属性即可。

    control.DataBindings.Add("BackColor", myclass, "Color");
    

    注意:INotifyPropertyChanged 接口实现是必须的,只有当你的属性发生变化时才会立即反映在绑定中。

    【讨论】:

    • 你确定这是 System.Windows.Forms 吗?因为它看起来就像 WPF。
    • @nvoigt 是什么让您认为这是 WPF?这绝对是System.Windows.Forms.dll 中的“Winforms”。顺便说一句,我不熟悉 WPF :) 我只是在发布之前用Form 对其进行了测试,它可以工作。
    • 你是对的,WinForms 数据绑定的工作方式似乎比我想象的更像 WPF。从来不知道我知道 WinForms 绑定 :)
    猜你喜欢
    • 2011-06-21
    • 2011-07-06
    • 2011-08-29
    • 2020-12-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多