I'm trying to make it so that when a particular property of an object gets changed then another propery also updates. The change I am trying to do is that when a string property eg "10^-1" changes to "10^-2" then another int proprty goes from 10 to 100.
I expect I can listen for the OnPropertyChanged event somehow but I'm fairly new to C# and WPF and haven't found the correct search term to find what (I think) I'm looking for.
I have an abstract class implementing INotifyPropertyChanged:
public abstract class ObservableObject : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
/// <summary>
/// Raises the PropertyChange event for the property specified
/// </summary>
/// <param name="propertyName">Property name to update. Is case-sensitive.</param>
public virtual void RaisePropertyChanged(string propertyName)
{
OnPropertyChanged(propertyName);
}
/// <summary>
/// Raised when a property on this object has a new value.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var ev = new PropertyChangedEventArgs(propertyName);
handler(this, ev);
}
}
Which is used in the object class:
public class SampleModel : ObservableObject, ISampleObject
{
#region SampleModel Fields
private string _dilutionFactorScientific;
private int _dilutionFactor;
#endregion SampleModel Fields
#region SampleModel Properties
public string DilutionFactorScientific
{
get { return _dilutionFactorScientific; }
set
{
if (value != null)
{
_dilutionFactorScientific = value;
OnPropertyChanged("DilutionFactorScientific");
}
}
}
public int DilutionFactor
{
get { return _dilutionFactor; }
set
{
if (value != null)
{
_dilutionFactor = value;
OnPropertyChanged("DilutionFactor");
}
}
}
#endregion SampleModel Properties
}
The ISampleObject interface just specifies the properties.
I also have a view model that holds a collection of the objects but I feel like that shouldn't be where the event gets acted upon (?)
public class SampleViewModel : ObservableObject, IPageViewModel
{
#region SampleViewModel Fields
private ObservableCollection<ISampleObject> _sampleModels;
private ISampleObject _currentSampleAliquot;
#endregion SampleViewModel Fields
#region SampleViewModel Properties
public ISampleObject CurrentSampleAliquot
{
get
{
return _currentSampleAliquot;
}
set
{
if (value != CurrentSampleAliquot)
{
_currentSampleAliquot = value;
OnPropertyChanged("CurrentSampleAliquot");
}
}
}
public ObservableCollection<ISampleObject> SampleModels
{
get
{
if (_sampleModels == null)
{
_sampleModels = new ObservableCollection<ISampleObject>();
OnPropertyChanged("SampleModels");
}
return _sampleModels;
}
set
{
}
}
#endregion SampleViewModel Properties
#region SampleViewModel Methods
/* code */
#endregion SampleViewModel Methods
}
The property is being updated via a xaml UI, which I have not included since the initial property seems to be getting updated just fine.