I found a difference on property binding between UserControl and a normal Control.
For example, assuming that the markup contains the following usercontrol:
<myCtrl:DemoControl Level="{Binding Alarm.AlarmLevel}" />
"Level" is an int dependency property created in "Control". "Alarm" is an object of type Inotifypropertychanged, with a field AlarmLevel.
public bool AlarmLevel
{
get
{
return this._alarmLevel;
}
set
{
this._alarmLevel = value;
NotifyPropertyChanged("AlarmLevel");
}
}
Inside the usercontrol, I did the following:
LevelProperty = DependencyProperty.Register("Level", typeof(int), typeof(DemoControl), new UIPropertyMetadata(0, isLevelChanged));
The strange thing is that when assign AlarmLevel to a value, if the value changes, the usercontrol property got updated. While if value remains the same, no update. BUT IN BOTH CASES, "NotifyPropertyChanged" gets called !
For example, if AlarmLevel==1,
Alarm.AlarmLevel = 2; // the "isLevelChanged" got called
Alarm.AlarmLevel = 1; // the "isLevelChanged" not called
I remember that with the normal control, whenever PropertyChanged is called, the property gets updated. Anybody knows why? Many thanks!
DependencyProperty. TheDependencyPropertyhas some logic inside and only updates if the value changes. That avoids unnecessary updates of the GUI (which are quite costly). On the other hand there are rare cases where you need to work around that behavior. - gomi42