0

I have a TabControl with a ContentTemplate defining a CheckBox. Outside the TabControl I want to bind to the CheckBox in the currently selected TabItem. How can I do that?

In the code below the CheckBox "Mirror" is bound to a CheckBox on the same level, which works. How can I make the Mirror-CheckBox to mirror tabCheckbox?

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TabControl Grid.Row="0">
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <CheckBox
                        Name="tabCheckbox"
                        Content="Check me"
                        />
                </DataTemplate>
            </TabControl.ContentTemplate>

            <TabItem Header="Tab 1">
            </TabItem>
        </TabControl>

        <CheckBox
            x:Name="checkbox"
            Grid.Row="1"
            Content="Some CheckBox"
            />
        <CheckBox
            Grid.Row="2"
            Content="Mirror"
            IsChecked="{Binding ElementName=checkbox, Path=IsChecked}"
            />
    </Grid>
Johannes Schacht
  • 930
  • 1
  • 11
  • 28

2 Answers2

0

It make sense to bind both CheckBoxes to the same ViewModel property. This way you will be able to bind content only of the focused tab to the target view model that is used by the standalone CheckBox.

Drreamer
  • 332
  • 1
  • 10
  • Actually my real problem is a bit different one. The sample is a simplification. The real problem is binding against a property that exists in the view only. – Johannes Schacht Apr 30 '20 at 19:56
  • May be a binding using a RelativeSource FindAncestor: https://stackoverflow.com/a/84317/12797700. I am afraid your task is not quite clear to me, and I cannot suggest something more precise – Drreamer Apr 30 '20 at 21:25
0

Binding to an element inside of a DataTemplate from outside of it is not possible, as far as I know. But you can instead bind from the mirror checkbox to the tabCheckbox.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TabControl Grid.Row="0">
        <TabControl.ContentTemplate>
            <DataTemplate>
                <CheckBox
                    x:Name="tabCheckbox"
                    Content="Check me"
                    IsChecked="{Binding IsChecked, Source={x:Reference mirrorCheckBox}}" />
            </DataTemplate>
        </TabControl.ContentTemplate>

        <TabItem Header="Tab 1">
        </TabItem>
    </TabControl>

    <CheckBox
        x:Name="mirrorCheckBox"
        Grid.Row="2"
        Content="Mirror"
        />
</Grid>

x:Reference allows you to reference elements directly by their names in such situations.

Igor
  • 600
  • 6
  • 13
  • Note that XAML hot reload will not work if you update such reference. You will have to restart the application. – Igor Apr 30 '20 at 20:08