Reputation: 466
Suppose in a C# WPF class I have something like this:
class test: INotifyPropertyChanged
{
private datetime _DOB
public datetime DOB
{
get
{
return _DOB;
}
set
{
if (_DOB != value)
{
_DOB = value;
RaisePropertyChanged();
}
}
}
private int _age
public int Age
{
get
{
return _age;
}
set
{
if (_age != value)
{
_age = value;
RaisePropertyChanged();
}
}
}
....
}
Now, let us suppose that a user can put in any of the properties. They can put in either their DOB, or their age, so making one of them calculated property would not work.
Apart from using methods like UpdateAge()
in the setter of DOB and UpdateDOB()
in the setter of Age, is there any other method that would automatically update the "dependent" properties along with the dependency properties (which INPC takes care of)?
Upvotes: 0
Views: 2752
Reputation: 169150
is there any other method that would automatically update the "dependent" properties along with the dependency properties (which INPC takes care of)?
There is no "automatic" or magic here. You need to set the backing field and raise the PropertyChanged event for the data bound property that you want to refresh in the view.
Just make sure that you don't end up in an infinite loop where you set the Age
property from the DOB
property and vice versa.
You could set the _age
backing field in the setter of the DOB
property and vice versa, e.g.:
private DateTime _DOB;
public DateTime DOB
{
get
{
return _DOB;
}
set
{
if (_DOB != value)
{
_DOB = value;
RaisePropertyChanged();
_age = DateTime.Now.Year - _DOB.Year;
RaisePropertyChanged("Age");
}
}
}
Upvotes: 1