Reputation: 63
I've created a DependencyProperty on my derived AutoCompleteBox control --> IsReadOnly
From there, I'm trying to set the value (T/F) via a converter. Based on the converter value, I would like to update the nested TextBox style in the setter of the DependencyProperty. Explicitly setting the property in the XAML (IsReadOnly="True") works fine, and the setter fires and updates the style. However, doing this via the converter does NOT fire of the setter of the DependencyProperty. I seem to be having trouble pasting code snippets here (first time poster).. so I'll do my best to give an quick code run through:
Property on AutoCompleteBox:
IsReadOnly="{Binding Converter={StaticResource IsReadOnlyVerifier}, ConverterParameter='Edit Client'}"
Which calls out to the Converter, which returns either true or false based on the User's permissions. This however does not call the setter of the registered DependencyProperty.
.. set
{
if (value)
{
var style = StyleController.FindResource("ReadOnlyTextBox") as Style;
TextBoxStyle = style;
}
else
{
TextBoxStyle = null;
}
SetValue(IsReadOnlyProperty, value);
}
Upvotes: 3
Views: 721
Reputation: 189457
This is a classic newbie gotcha. Bindings will set the target DependencyProperty
using SetValue
directly, they don't assign a value via the POCO property setter method.
Your IsReadOnly
property should look like this:-
#region public bool IsReadOnly
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyProperty); }
set { SetValue(IsReadOnlyProperty, value); }
}
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register(
"IsReadOnly",
typeof(bool),
typeof(MyAutoCompleteBox),
new PropertyMetaData(false, OnIsReadOnlyPropertyChanged) );
private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyAutoCompleteBox source = d as MyAutoCompleteBox;
source.OnIsReadOnlyChanged((bool)e.OldValue, (bool)e.NewValue);
}
private void OnIsReadOnlyChanged(bool oldValue, bool newValue)
{
TextBoxStyle = newValue ? StyleControlller.FindResource("ReadOnlyTextBox") as Style ? null;
}
#endregion
It affect any other changes when a dependency property is set you should supply a PropertyChangedCallback
delegate to the PropertyMetaData
when registering the DependencyProperty
. This will be called whenever SetValue
is used to assign a value for this property.
Upvotes: 3