WPF usercontrol property binding to Inotifypropertychanged, update only when value changed -


i found difference on property binding between usercontrol , normal control.

for example, assuming markup contains following usercontrol:

<myctrl:democontrol level="{binding alarm.alarmlevel}" /> 

"level" int dependency property created in "control". "alarm" object of type inotifypropertychanged, field alarmlevel.

public bool alarmlevel {          {         return this._alarmlevel;     }     set     {         this._alarmlevel = value;         notifypropertychanged("alarmlevel");     } } 

inside usercontrol, did following:

levelproperty = dependencyproperty.register("level", typeof(int), typeof(democontrol), new uipropertymetadata(0, islevelchanged)); 

the strange thing when assign alarmlevel value, if value changes, usercontrol property got updated. while if value remains same, no update. but in both cases, "notifypropertychanged" gets called !

for example, if alarmlevel==1,

alarm.alarmlevel = 2; // "islevelchanged" got called alarm.alarmlevel = 1; // "islevelchanged" not called 

i remember normal control, whenever propertychanged called, property gets updated. knows why? many thanks!

there bug in alarmlevel setter. should be:

if (_alarmlevel != value) { this._alarmlevel = value; notifypropertychanged("alarmlevel"); } 

you should raise inotifypropertychanged when value changes. when use inotifypropertychanged, change check responsibility. when you dependency properties, wpf framework check you.

that's why code half working :).


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -