Below tabitem3 is working fine.
I would like to get away from naming the controls and pass the properties by RelativeSource.
The code that is failing is
Binding RelativeSource="{RelativeSource AncestorType={x:Type Expander}}" Path="IsExpanded"
The error in the converter is dependency object not set
The Expander is a sibling not an ancestor.
How can I find that sibling (without an x:Name)?
<TabItem x:Name="tabitem3" IsSelected="False">
<TabItem.Header>
<Expander x:Name="tabexp3" Header="Three" IsHitTestVisible="True"
Expanded="expcolp" Collapsed="expcolp" IsExpanded="False"/>
</TabItem.Header>
<TextBlock Text="Content Three TabItem" Background="LightBlue" >
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource bvc2}" Mode="OneWay">
<Binding ElementName="tabexp3" Path="IsExpanded"/>
<Binding ElementName="tabitem3" Path="IsSelected" />
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
</TabItem>
<TabItem IsSelected="False">
<TabItem.Header>
<Expander Header="Four" IsHitTestVisible="True"
Expanded="expcolp" Collapsed="expcolp" IsExpanded="False"/>
</TabItem.Header>
<TextBlock Text="Content Four TabItem" Background="LightBlue" >
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource bvc2}" Mode="OneWay">
<Binding RelativeSource="{RelativeSource AncestorType={x:Type Expander}}" Path="IsExpanded"/>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type TabItem}}" Path="IsSelected"/>
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
</TabItem>
I am not sure that Expander is a real sibling of the TextBlock. The first one is a child of TabItem's header, the former is a child of TabItem's content.
Anyway if you do not want to use naming (indeed I do not like it too), you can "go up" through the logical tree by looking for a TabItem
ancestor and then you can "go down" by using the right path.
The result is this binding:
<Binding RelativeSource="{RelativeSource AncestorType={x:Type TabItem}}" Path="Header.IsExpanded" />
I hope it can help you.
EDIT
To test my binding you can use this simple XAML:
<TabControl>
<TabItem IsSelected="True">
<TabItem.Header>
<Expander Header="One" IsHitTestVisible="True" IsExpanded="False"/>
</TabItem.Header>
<TabItem.Content>
<TextBlock Text="Some contents..." />
</TabItem.Content>
</TabItem>
<TabItem IsSelected="False">
<TabItem.Header>
<Expander Header="Two" IsHitTestVisible="True" IsExpanded="False"/>
</TabItem.Header>
<TabItem.Content>
<TextBlock>
<TextBlock.Text>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type TabItem}}" Path="Header.IsExpanded" />
</TextBlock.Text>
</TextBlock>
</TabItem.Content>
</TabItem>
</TabControl>
If you expand/collapse the second Expander, the TextBlock's text will change.
My binding does not work if the TabItem has IsSelected
set to true. In this case you can extend my idea in this way:
<Binding RelativeSource="{RelativeSource AncestorType={x:Type TabControl}}" Path="SelectedItem.Header.IsExpanded" />