Search code examples
c#wpfdata-bindingdatatemplatetabcontrol

DataTemplate does not show up with TabControl


i am using a DataBinding to show all Controls in the ItemsSource

        <TabControl Grid.Row="1" TabStripPlacement="Left" ItemsSource="{Binding Source={StaticResource WorkflowSelector}, Path=Workflows}" SelectedIndex="0">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding PluginName}"></Label>
            </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>

the WorkflowSelector is my ViewModel which contains a List of all Controls that should be shown in the TabControl.

I created an itemTemplate to show the PluginName (public property) in the Tab but nothing is shown.

If i inspect the Visual tree i can see the Databinding of the Tabcontrol, containing 1 Item that has a Property PluginName. The evaluated Value of the BindingExpression of the Label is empty

first thing i noticed is that the ContentPresenter does not have a DataContext, while the Border does have the correct DataContext

Visual Tree of the application; ContentPresenter does not hold the DataContext

Additionally ... if i change the ItemTemplate to ContentTemplate the binding is working correctly (but in the content not in the header)


Solution

  • since i saw in the Visual tree that the DataContext was set higher up in the hierarchy, i changed my data binding to:

     <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Path=DataContext.PluginName}"></Label>
    

    this does not explain the root-cause, but it is a short workaround i can live with