In order for this to work, the class of the object being examined must implement the INotifyPropertyChanged interface, and the property being examined must trigger an event upon changes.
A small demo to understand:
class Program : INotifyPropertyChanged { public int Prop1 { get; set; } private int _prop2 public int Prop2 { get { return _prop2; } set { _prop2 = value; OnPropertyChanged("Prop2"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } private static void P_PropertyChanged(object sender, EventArgs e) { Console.WriteLine(e.GetType().ToString()); Console.WriteLine(sender.GetType().ToString()); if (e is PropertyChangedEventArgs) { var realArgs = (PropertyChangedEventArgs)e; Console.WriteLine(realArgs.PropertyName); } } static void Main(string[] args) { var p = new Program(); var props = TypeDescriptor.GetProperties(p); props[nameof(p.Prop1)].AddValueChanged(p, P_PropertyChanged); props[nameof(p.Prop2)].AddValueChanged(p, P_PropertyChanged); p.Prop1 = 1; p.Prop2 = 2; Console.ReadLine(); } }
If you start and see the output of the program, it is easy to notice that for the Prop1 property the handler is not called, since This property does not report changes. For the property Prop2 when changing, the handler is called, since we have made sure in advance that the property notifies of changes and that the event handler is not passed empty EventArgs , but PropertyChangedEventArgs which contains the name of the property.
The only question that remains is: why such difficulties, if you can immediately subscribe to the PropertyChanged event, which is defined in the INotifyPropertyChanged interface without which all this is generally useless? Check that the class implements the interface and subscribe directly to the event without intermediaries seems to me much easier and more understandable.
object sender, EventArgs e? - 4per