Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyProperty bool in UserControl

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?

like image 565
SimonHL Avatar asked Aug 14 '26 17:08

SimonHL


1 Answers

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.

like image 188
dpalmajr Avatar answered Aug 17 '26 08:08

dpalmajr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!