-2

Following is my code

                <myusrcontrol:settings x:Name="usr1" />

        </TabItem>
        <TabItem>
            <TabItem.Content>

                    <Grid>
                        <myusrcontrol:abcselection  x:Name="usr2"  Height="Auto" />
                    </Grid>

            </TabItem.Content>
        </TabItem>

In the above code, I want to disable "usr2" with respect to a CheckBoxControl-Checked Property found in "usr1" through xaml only,

Kindly let me know if there is any solution,

Thanks in Advance

  • @Slyvain ,it's not duplicate ,here it's different usercontrols – SiddarthVarunesh May 25 '16 at 09:45
  • The problem is exactly the same: you have a custom control `usr2` taht you wish disabled or enabled based on the `CheckBoxControl-Checked` property of your `usr1` control. Change the names of controls and properties, and you have your solution, the key is the binding on ElementName. – Slyvain May 25 '16 at 09:50
  • @Slyvain , ok can you tell me how to call the inner element of a usercontrol like "ElementName = usr1.CheckBox1" . I tried this but it's not working – SiddarthVarunesh May 25 '16 at 10:35

1 Answers1

0

The situation is a little bit more complex than in the post I mentioned as duplicate, so here is a working solution:

As far as I know, you need to create a DependencyProperty (DP) on your UserControl with the Checkbox and bind the Checkbox.IsChecked property to this DP. Then you can bind your usr2 control on the DP.

Here is the code:

usr1.xaml

<UserControl [...]>
   <Grid>
       <CheckBox x:Name="MyCheckbox" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:usr1}}, Path=MyCheckboxIsChecked, Mode=TwoWay}" />
   </Grid>
</UserControl>

usr1.xaml.cs (code behind)

public partial class usr1: UserControl
   {
      public usr1()
      {
         InitializeComponent();
      }

      public static readonly DependencyProperty MyCheckboxIsCheckedProperty = DependencyProperty.Register(
       "MyCheckboxIsChecked", typeof(bool), typeof(usr1), new PropertyMetadata(default(bool)));

      public bool MyCheckboxIsChecked
      {
         get
         {
            return (bool)GetValue(MyCheckboxIsCheckedProperty);
         }
         set
         {
            SetValue(MyCheckboxIsCheckedProperty, value);
         }
      }
   }

usr2.xaml

<UserControl [...]>
    <Grid>
         <TextBox x:Name="MyTextBox" />
    </Grid>
</UserControl>

MainWindow.xaml

<Grid>
      <StackPanel>
         <local:usr1 x:Name="usr1Control" />
         <local:usr2 IsEnabled="{Binding ElementName=usr1Control, Path=MyCheckboxIsChecked}" />
      </StackPanel>
   </Grid>
Slyvain
  • 1,732
  • 3
  • 23
  • 27