c# - Propertychanged event firing even when property value not changed -


i bind properties controls in wpf. values of properties assigned/changed recursive method in while loop. values assigned @ rate of ~ 1 ms. of time values not changed @ propertychanged event in setter fires when property value not changed. thinking property setter should raise event only when value of field going change. here simplified version of 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    {        { return _somebool1; }        set        {           _somebool1 = value;            onpropertychanged("somebool1");             //messagebox.show("somebool1 : propertychanged event fired!");        }    } } 

according http://www.codemag.com/article/0907101 ui consumer of propertychanged events. while values of many properties assigned , fast possible result in unnecessary ui overhead. ok place onpropertychanged("somebool2"); in if statement? :

private bool _somebool2 = false; public bool somebool2 {     { return _somebool2; }     set     {          bool _somebool2oldvalue = _somebool2;          _somebool2 = value;           if (_somebool2 != _somebool2oldvalue)          {              onpropertychanged("somebool2");              //messagebox.show("somebool2 : propertychanged event fired!");          }      } } 

have misunderstood idea "fire event when property value changed" or code implementation wrong?

i can't imagine, stuff somebool2oldvalue intended for:

set {      if (_somebool2 != value)      {          _somebool2 == value;          onpropertychanged("somebool2");      }  } 

moreover, if you're running loop, brings view model frequent property changes, makes sense stop firing propertychanged event @ all:

protected bool stopfiringpropertychanged { get; set; }  protected virtual void onpropertychanged(string propertyname) {     if (stopfiringpropertychanged)      {         return;     }      // fire event } 

and fire after loop finished properties, changed during loop:

private void somemethodwithrecursiveloop() {     stopfiringpropertychanged = true;     try     {          // work     }         {         stopfiringpropertychanged = false;         onpropertychanged("someproperty1");         onpropertychanged("someproperty2");         onpropertychanged("someproperty3");     }    } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -