I use both approaches depending on the property relationship.
If it would be logical that changing one property would trigger a change in another, I just leave the property change code in the setter.
public string PropertyA
{
get { return propertyA; }
set
{
myPropertyA = value;
RaisePropertyChanged(nameof(PropertyA));
RaisePropertyChanged(nameof(IsPropertyAValid));
...
}
}
However if the two properties are not logically related, then I will use the PropertyChange handler :
private void MyClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch(e.PropertyName)
{
case "PropertyA":
RaisePropertyChanged(nameof(PropertyB));
break;
}
}
The reason for this is I want no special logic in my property setters. To me, they should be fairly generic, stuck in a #region
tag, and collapsed to be forgotten about.
If two properties are logically related and you would expect changing one to potentially alter the value of another, then the RaisePropertyChange
code can be left in the setter.
But if they are different, and in the future myself or another dev were looking at the code and would potentially not know/understand that there's a dependency between the two, I put the change in the class's PropertyChange event handler so it's easy to find and/or change if necessary.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…