[英]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.