I have a problem with a DependencyProperty in a UserControl. My control exposes two Dependencyproperties, a bool and a string. The string property works, but the bool doesn't. I get no errors, but changes aren't reflected either way.
I define the property like this:
private static readonly DependencyProperty IncludeSubdirectoriesProperty =
DependencyProperty.Register(
"IncludeSubdirectories",
typeof(bool),
typeof(DirectorySelect),
new FrameworkPropertyMetadata(false) { BindsTwoWayByDefault = true }
);
public bool IncludeSubdirectories
{
get { return (bool) GetValue(IncludeSubdirectoriesProperty); }
set { SetValue(IncludeSubdirectoriesProperty, value); }
}
In the XAML for the user control i bind to the property like this:
<CheckBox
Name="IncludeSubdirectoriesCheckbox"
IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
Include subfolders</CheckBox>
And when i use the control i bind to the properties like this:
<Controls:DirectorySelect
Directory="{Binding Directory}"
IncludeSubdirectories="{Binding WatchSubDirs}"/>
"Directory" is the string property that works just fine. I bind to them both in the same way, but i just can't make the bool work.
Where did i go wrong?
You could try chaning the binding with your user control to an element binding. Before you do be sure to give your userControl a name.
Then change:
IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
To something like this:
IsChecked="{Binding Path=IncludeSubdirectories, ElementName="<UserControlName>", Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
Another sanity check you can perform is to make sure the type owner for IncludeSubdirectoriesProperty' is correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With