简体   繁体   English

如何在Winforms控件上设置自定义属性可绑定?

[英]How to set custom properties on a Winforms control bindable?

I have some properties like OverlayColor, etc that I want to bind to an instance of a different type, but the bound data just doesn't change. 我有一些像OverlayColor等属性,我想绑定到不同类型的实例,但绑定数据不会改变。

I use this: 我用这个:

[Bindable ( true )]
public Color OverlayColor { get; set; }

The UI changes but not the bound data. UI更改但不更改绑定数据。 Bound data's property name is Color. 绑定数据的属性名称为Color。

As I understand the Bindable attribute is to add the property under the (DataBindings) for the current control. 据我所知,Bindable属性是在当前控件的(DataBindings)下添加属性。

To resolve the issue that you have where the OverlayColor is not updated on the binding, you have to implement the INotifyPropertyChanged interface on the object that you're binding to. 若要解决在绑定上未更新OverlayColor的问题,必须在要绑定的对象上实现INotifyPropertyChanged接口。 When the binded object is changed you have to Raise the NotifyPropertyChanged event. 绑定对象更改后,您必须提升NotifyPropertyChanged事件。

In the example below I created a Data class which I use to bind to and call the ChangeColor() method to change the color. 在下面的示例中,我创建了一个Data类,我用它来绑定并调用ChangeColor()方法来更改颜色。

public class Data : INotifyPropertyChanged
{
  Color overlayColor = Color.Teal;

  public event PropertyChangedEventHandler PropertyChanged;

  public Data()
  {
  }

  public Color OverlayColor
  {
    get
    {
      return overlayColor;
    }
    set
    {
      overlayColor = value;
      NotifyPropertyChanged( "OverlayColor" );
    }
  }

  public void ChangeColor()
  {
    if ( OverlayColor != Color.Tomato )
      OverlayColor = Color.Tomato;
    else
      OverlayColor = Color.DarkCyan;
  }

  private void NotifyPropertyChanged( string propertyName )
  {
    if ( PropertyChanged != null )
      PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 xamarin 自定义控件中的 2 个可绑定属性相互识别? - How to make 2 bindable properties in a xamarin custom control aware of each other? 从视图设置时自定义控件可绑定属性未命中 - Custom control bindable Property not getting hit when set from a view 自定义控件条目未设置占位符颜色可绑定属性 - Custom Control Entry not getting set the Place Holder Color Bindable Property C#WinForms自定义控件默认属性 - C# WinForms custom control default properties 如何在DevExpress(Winforms)自定义外观中设置属性的选择? - How do I set a selection of properties in DevExpress (Winforms) custom skin? 如何在Winforms的自定义控件中设置Colors的默认值? - How to set the default value of Colors in a custom control in Winforms? 如何使自定义XAML用户控件可绑定? - How do you make a custom XAML User Control bindable? 如何将 object 作为可绑定属性传递给 Xamarin.Forms 自定义控件 - How to pass object as bindable property to Xamarin.Forms custom control 如何在WinForms自定义控件的控件设计器属性中添加组合框? - How can I add a combobox in control designer properties for a WinForms custom control? 如果控件位于ToolStripControlHost内部,如何设置DataGridView的Height属性? C#WINFORMS - How to set DataGridView's Height properties if the control is inside a ToolStripControlHost? C# WINFORMS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM