I bind properties to controls in WPF. Values of properties are assigned/changed by recursive method which is in while loop. So values are assigned at rate of ~ 1 ms. Most of the time values are not changed at all but propertychanged event in setter fires even when property value is not changed. I was thinking that property setter should raise an event only when the value of a field is going to change. Here is simplified version of my code:
public sealed class FX : System.ComponentModel.INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string PropertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(PropertyName)); } } private bool _someBool1 = false; public bool SomeBool1 { get { return _someBool1; } set { _someBool1 = value; OnPropertyChanged("SomeBool1"); //MessageBox.Show("SomeBool1 : propertychanged event fired!"); } } } According to http://www.codemag.com/Article/0907101 UI is the consumer of PropertyChanged events. While values of many properties are assigned constantly and as fast as possible that could result in unnecessary UI overhead. Is it ok to place OnPropertyChanged("SomeBool2"); in if statement? :
private bool _someBool2 = false; public bool SomeBool2 { get { return _someBool2; } set { bool _someBool2OldValue = _someBool2; _someBool2 = value; if (_someBool2 != _someBool2OldValue) { OnPropertyChanged("SomeBool2"); //MessageBox.Show("SomeBool2 : propertychanged event fired!"); } } } Have I misunderstood the idea "fire an event when property value is changed" or my code implementation is wrong?