0

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.

Hulikoi
  • 43
  • 1
  • 12
  • 1
    Why do you not put the code you want in the setter of the property. Obviates the need to listen to any event. – Jonathan Willcock Jun 14 '21 at 21:00
  • You really only have a couple of options: subscribe to `PropertyChanged` yourself and handle it just like any other code would, or add code in the setter to directly provide an equivalent mechanism. See duplicate for discussion of these two options. Note that in some implementations of `INotifyPropertyChanged` (e.g. an `ObservableObject`-like base class), the method that handles raising the event will also provide a mechanism for a callback delegate to be invoked at the same time. See e.g. my answer at https://stackoverflow.com/a/39419253/3538012 – Peter Duniho Jun 14 '21 at 21:07
  • @JonathanWillcock, That is such a simple answer it did not occur to me, I'll give that a try in the first instance. – Hulikoi Jun 14 '21 at 21:14
  • @PeterDuniho, thanks for that I'll take a look at that answer as well. cheers – Hulikoi Jun 14 '21 at 21:15

0 Answers0