I am not sure why the property is not being called on Binding. Here is the code:
<myusercontrol
Text ="{Binding Description, UpdateSourceTrigger=LostFocus,Mode=TwoWay, ValidatesOnDataErrors=True}"
IsReadOnly ="{Binding AllowEditing}"
/>
And here is the myusercontrol IsReadOnly property:
public static DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof (bool),
typeof (
myusercontrol));
public bool IsReadOnly
{
get
{
return ((bool) GetValue(IsReadOnlyProperty));
}
set
{
MessageBox.Show(value.ToString());
SetValue(IsReadOnlyProperty, !value);
OnPropertyChanged("IsReadOnly");
}
}
The message box is never displayed! Any ideas!
You should never put any logic in your dependency property getters and setters except for the
GetValueandSetValuecalls. This is very important, because the XAML binding will go directly through theGetValueandSetValuecalls, not through your code-behind property! That is why you are never seeing theMessageBox. A better approach is to add a call-back method using theDependencyProperty.Registermethod (there is an overload to add a call-back). Then, that method will be called whenever the value changes, and you can place your logic there.Another question- why are you using
OnPropertyChanged? Dependency properties have change notification built-in, you should never have to callOnPropertyChangedfor them.