How to have bindable properties of a UserControl which work with OnPropertyChanged

前端 未结 2 1624
情书的邮戳
情书的邮戳 2021-02-20 12:48

I have a simple usercontrol (WinForms) with some public properties. When I use this control, I want to databind to those properties with the DataSourceUpdateMode set to

2条回答
  •  無奈伤痛
    2021-02-20 13:07

    Implementing the INotifyPropertyChanged interface is very simple. Here is a sample that shows an object with a single public field...

    public class Demo : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    
        private string _demoField;
    
        public string DemoField
        {
            get {return demoField; }
    
            set
            {
                if (value != demoField)
                {
                    demoField = value;
                    NotifyPropertyChanged("DemoField");
                }
            }
        }
    }
    

    Then you would create a Binding instance to bind a control property to a property (DemoField) on your source instance (instance of Demo).

提交回复
热议问题