How to set custom properties in a Winforms control?

I have some properties, such as OverlayColor, etc. that I want to bind to an instance of a different type, but the associated data just doesn't change.

I use this:

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

The user interface modifies, but not related data. The associated data property name is Color.

+3
source share
1 answer

As I understand it, the Bindable attribute should add a property under (DataBindings) for the current control.

, OverlayColor , INotifyPropertyChanged , . , " NotifyPropertyChanged".

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 ) );
  }
}
+5

Source: https://habr.com/ru/post/1721238/


All Articles