I have 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> <TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/> </TabItem.Header> <TabItem.ContentTemplate> <DataTemplate> <TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/>
This custom TabItem defines the DependencyProperty ShortLabel to implement the interface. I would like to bind to this and other properties from the TabItem DataTemplate . But due to weird interactions, the TextBlock inside the DataTemplate bound to the parent TabItem container , which is also called βIβ, but defined in another Xaml file.
Question
Why does binding work in TabItem.Header but not from inside TabItem.ContentTemplate, and how do I get to user control properties from a DataTemplate?
What have i tried
TemplateBinding : Attempts to bind to ContentPresenter inside the gut TabItem .FindAncestor, AncestorType={x:Type TabItem} : The TabItem parent was not found. This also does not work when I specify the type MyTabItem .ElementName=Self : ElementName=Self to associate a control with this name in the wrong scope (parent container, not TabItem ). I think this gives us a hint why this does not work: a DataTemplate is not created at the point where it is defined in XAML, but apparently by the parent container.
I suppose I can replace the entire ControlTemplate to achieve the effect I'm looking for, but since I want to keep the TabItem look default, without having to support the entire ControlTemplate , I am very reluctant to do this.
Edit
Meanwhile, I found out that the problem is this: TabControl cannot have (any) ItemsTemplate (including DisplayMemberPath ) if the ItemsSource contains Visual s. There is a thread on the MSDN forum explaining why .
Since this seems to be the main issue with WPF TabControl, I close the question. Thank you for your help!
wpf binding xaml datatemplate
David schmitt
source share