简体   繁体   English

将属性更改事件添加到自定义控件

[英]Adding property changed event to a custom control

For an application I have to use a custom button that react when one of its properties value is being changed. 对于一个应用程序,我必须使用一个自定义按钮,该按钮在其属性值之一更改时会做出反应。 I addded a field named Data to the new button: 我在新按钮中添加了一个名为Data的字段:

public class ButtonData
{
    public string Name;
    public string Color;
    //And more stuff...
}

Then I have the folowing code for new button, I want it to be able to update itself (change background color and some other stuff) whenever its Data property gets updated from somewhere in application. 然后,我有了下面的新按钮代码,我希望它能够在应用程序中的某个位置更新其Data属性时进行自身更新(更改背景色和其他内容)。 I found some ideas about implementing INotifyPropertyChanged interface and I set it up in my custom button like this: 我发现了一些有关实现INotifyPropertyChanged接口的想法,并在我的自定义按钮中进行了设置,如下所示:

public partial class ButtonPin : Button, INotifyPropertyChanged
{
    private ButtonData _data;

    public ButtonData Data
    {
        get { return _data; }
        set
        {
            if (value == _data) return;
            _data = value;
            OnPropertyChanged("Data");
        }
    }
    private bool _buttonDataAdded;


    public ButtonPin()
    {
        InitializeComponent();
    }

    public ButtonPin(ButtonData data)
    {
        Data = data;
        _buttonDataAdded = true;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
  }

Now I am not sure how to use this! 现在我不确定如何使用它! For example, if the Color in Data objects gets changed somehow somewhere and its get assigned to current button's Data field, I want this button to change its background color. 例如,如果“ Data的颜色”对象在某处进行了某种更改并将其分配给当前按钮的“数据”字段,我希望此按钮更改其背景颜色。 Something like 就像是

var data = new ButtonData();
data.Name = "Hi!";
data.Color = Color.Red;

buttonPin1.Data = data; //Here I need the changes to occur

You have implemented the interface INotifyPropertyChanged on the ButtonPin class, not on the ButtonData class, and you want to detect a change on an object of type ButtonData, thus you need to implement the interface INotifyPropertyChanged on the ButtonData class. 您已经在ButtonPin类而不是ButtonData类上实现了INotifyPropertyChanged接口,并且您想检测到ButtonData类型的对象上的更改,因此需要在ButtonData类上实现INotifyPropertyChanged接口。

To detect the change you need to hook up to the PropertyChanged event of the ButtonData object in the setter of the ButtonPin.Data property. 要检测更改,您需要连接到ButtonPin.Data属性的设置器中ButtonData对象的PropertyChanged事件。 Something like this. 这样的事情。

private bool _data;
public ButtonData Data {
    get { return _data; }
    set {
        if (value != _data) {
            // Unhook previous events
            if (_data != null)
                _data.PropertyChanged -= HandleButtonDataPropertyChanged;
            // Set private field
            _data = value;
            // Hook new events
            if (_data != null)
                _data.PropertyChanged += HandleButtonDataPropertyChanged;
            // Immediate update  since we have a new ButtonData object
            if (_data != null)
                Update();
        }
    }
}

private void HandleButtonDataPropertyChanged(object sender, PropertyChangedEventArgs e) {
    // Handle change in ButtonData
    Update();
}

private void Update() {
    // Update...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM