I have a class that implements INotifyPropertyChanged as follows:
public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; string _color; public string Color { get{ return _color; } set { _color = value; RaisePropertyChanged(); } } ... private void RaisePropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }
When the set Color tool is called, it calls RaisePropertyChanged() , which automatically gets the property name ie "Color" and uses this to populate PropertyChangedEventArgs . Instead of manually entering the property name.
This is good because it prevents possible errors in your code, since you do not need to manually enter the property name. It also helps in refactoring your code, since you are not hard-coded for any lines.
My question
I have an event handler for PropertyChanged . How can I use the switch-case construct without hard coding Property names as strings. So something like this:
void Person_PropertyChanged(object sender, PropertyChangedEventArgs e){ switch (e.PropertyName) { case PropertyNameOf(Person.Color);
Is it possible? I want to do this in order to maintain the benefits that I mentioned above.
source share