需要将数据绑定到Winform控件BackColor的示例

时间:2017-04-25 17:52:12

标签: c# winforms data-binding

我一直在为我的C#Winforms应用程序GUI字段设置广泛使用数据绑定。现在我想使用相同的机制控制按钮颜色。但是我遇到了一个异常:"无法将值格式化为所需类型。" 相关的代码部分是:

public static UserInputs userInputs = new UserInputs();

public class UserInputs : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate (object sender, PropertyChangedEventArgs e){};

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null) {   
            try { PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) ); }
            catch (ArgumentOutOfRangeException e) { }
        }
    }
    [XmlIgnore] private System.Drawing.Color _applyGmmColor = System.Drawing.SystemColors.Control;
    [XmlIgnore] public System.Drawing.Color ApplyGmmColor {
        get { return _applyGmmColor; }
        set {
            _applyGmmColor = value;
            NotifyPropertyChanged();
        }
    }
}
buttonApplyGmm.DataBindings.Add("BackColor", userInputs, "ApplyGmmColor", true, DataSourceUpdateMode.OnPropertyChanged);

当我尝试通过更改userInputs.ApplyGmmColor的值来更改按钮背景颜色时,我得到以下异常:

System.FormatException was unhandled
  HResult=-2146233033
  Message=Cannot format the value to the desired type.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.Binding.FormatObject(Object value)
       at System.Windows.Forms.Binding.PushData(Boolean force)
       at System.Windows.Forms.Binding.UpdateIsBinding()
       at System.Windows.Forms.PropertyManager.UpdateIsBinding()
       at System.Windows.Forms.BindingManagerBase.PushData(Boolean& success)
       at System.Windows.Forms.PropertyManager.OnCurrentChanged(EventArgs ea)
       at System.Windows.Forms.BindToObject.PropValueChanged(Object sender, EventArgs e)
       at System.ComponentModel.PropertyDescriptor.OnValueChanged(Object component, EventArgs e)
       at System.ComponentModel.ReflectPropertyDescriptor.OnValueChanged(Object component, EventArgs e)
       at System.ComponentModel.ReflectPropertyDescriptor.OnINotifyPropertyChanged(Object component, PropertyChangedEventArgs e)
       at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
       at ADT.UserInputs.NotifyPropertyChanged(String propertyName) in C:\Users\rmpe01\Documents\GitHub_2017.1\AnomalyDetectionToolbox\ADT\UserInputs.cs:line 38
       at ADT.UserInputs.set_ApplyGmmColor(Color value) in C:\Users\rmpe01\Documents\GitHub_2017.1\AnomalyDetectionToolbox\ADT\UserInputs.cs:line 108
       at ADT.UseModel.setApplyButtonColor(ACTIVE_SENSOR_TYPE st, Color c) in C:\Users\rmpe01\Documents\GitHub_2017.1\AnomalyDetectionToolbox\ADT\UseModel.cs:line 31
       at ADT.UseModel.touch() in C:\Users\rmpe01\Documents\GitHub_2017.1\AnomalyDetectionToolbox\ADT\UseModel.cs:line 47
       at ADT.ADT.comboBoxGmmComputeInterval_SelectedIndexChanged(Object sender, EventArgs e) in C:\Users\rmpe01\Documents\GitHub_2017.1\AnomalyDetectionToolbox\ADT\Form1.cs:line 2370
       at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
       at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
       at System.Windows.Forms.ComboBox.set_Text(String value)
  InnerException: 

我发现了很多数据绑定控件值的例子,但很少影响控件属性,比如背景颜色。

我正在寻找一个简单示例,它演示了如何处理BackColor(或类似)控件属性,而不会在标准方法上添加大量额外代码。

提前感谢任何指导。

0 个答案:

没有答案