Home > Mobile >  How to bind IsEnabled properties of a parent to a CheckBox of a child in WPF?
How to bind IsEnabled properties of a parent to a CheckBox of a child in WPF?

Time:09-10

This works if CheckBox 'test1' is defined in the same page.

<Button IsEnabled="{Binding ElementName=test1, Path=IsChecked}" />

However, if I move CheckBox 'test1' to an UserControl and then try to bind it in such a way:

<Button IsEnabled="{Binding ElementName=userControlElementName, Path=test1.IsChecked}" />

I get no results when checking the 'test1' CheckBox.

What am I missing?

Edit

UserControl and Button are contained in the same View.

CodePudding user response:

According to your explanation, it seems to me that you are doing something wrong, since such a task arose.

But if you do not go into the essence of the general task, then for the behavior you require, you should add a property to UserControl, to which you can bind both a child and an external element.

Your UserControl:

    public partial class MyUC: UserControl
    {
        public bool ButtonIsEnabled {get; set;}
<Button IsEnabled="{Binding ButtonIsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyUC}}}"

Parent Page:

<local:MyUC x:Name="userControlElementName"/>
<Button IsEnabled="{Binding ButtonIsEnabled, ElementName=userControlElementName}"/>

P.S. If you need to change the value of ButtonIsEnabled from Sharp, then it should be declared as a DependencyProperty.

  • Related