I've got the following user control:
<TabItem
x:Name="Self"
x:Class="App.MyTabItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:App"
>
<TabItem.Header>
<!-- This works -->
<TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/>
</TabItem.Header>
<TabItem.ContentTemplate>
<DataTemplate>
<!-- This binds to "Self" in the surrounding window's namespace -->
<TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/>
This custom TabItem defines a DependencyProperty
'ShortLabel' to implement an interface. I would like to bind to this and other properties from within the TabItem
's DataTemplate
. But due to strange interactions, the TextBlock
within the DataTemplate
gets bound to the parent container of the TabItem
, which also is called "Self", but defined in another Xaml file.
Question
Why does the Binding work in the TabItem.Header, but not from within TabItem.ContentTemplate, and how should I proceed to get to the user control's properties from within the DataTemplate?
What I already tried
TemplateBinding
: Tries to bind to the ContentPresenter within the guts of theTabItem
.FindAncestor, AncestorType={x:Type TabItem}
: Doesn't find theTabItem
parent. This doesn't work either, when I specify theMyTabItem
type.ElementName=Self
: Tries to bind to a control with that name in the wrong scope (parent container, notTabItem
). I think that gives a hint, why this isn't working: the DataTemplate is not created at the point where it is defined in XAML, but apparently by the parent container.
I assume I could replace the whole ControlTemplate
to achieve the effect I'm looking for, but since I want to preserve the default look and feel of the TabItem
without having to maintain the whole ControlTemplate
, I'm very reluctant to do so.
Edit
Meanwhile I have found out that the problem is: TabControl
s can't have (any) ItemsTemplate
(that includes the DisplayMemberPath
) if the ItemsSource
contains Visual
s. There is a thread on MSDN explaining why. Since this seems to be a fundamental issue with WPF's TabControl, I'm closing the question. Thanks for all your help!