Question

For a WPF app using the ExpressionDark theme, I have a TabControl, and without any modifications, the theme style for the tab control is fine.

However, when I implement a HeaderTemplate via a Style Setter, this seems to override the theme's style for the header template.

What I want to do is to extend the theme's style so that my tab control's header can include an image (in the sample code below it's hard-coded but will ultimately be bound and dynamic).

Is there a way to indicate to the TabItemHeaderTemplate or TabItemHeaderTemplateSelected items to somehow do a BasedOn from the theme's template?

<UserControl x:Class="WpfApp1.ConfigControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"
         Background="{DynamicResource WindowBackgroundBrush}">

<UserControl.Resources>
    <DataTemplate x:Key="TabItemHeaderTemplate" >
        <Border>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" >
                <Image Height="32" Width="32"
                       Source="/Assets/Images/WarningIcon.png" />
                <TextBlock Text="{Binding}" VerticalAlignment="Center" Margin="10 0 0 0" FontSize="16" />
            </StackPanel>
        </Border>
    </DataTemplate>
    <DataTemplate x:Key="TabItemHeaderTemplateSelected">
        <Button>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" >
                <Image Height="32" Width="32"
                       Source="/Assets/Images/WarningIcon.png" />
                <TextBlock Text="{Binding}" VerticalAlignment="Center" Margin="10 0 0 0" FontSize="16" FontWeight="Bold" />
            </StackPanel>
        </Button>
    </DataTemplate>
    <DataTemplate x:Key="TabItemContentTemplate">
        <ContentControl Content="{Binding}" />
    </DataTemplate>
    <Style x:Key="TabItemContainerStyle" TargetType="TabItem" >
        <Setter Property="Header" Value="{Binding}"/>
        <Setter Property="HeaderTemplate" 
                Value="{StaticResource TabItemHeaderTemplate}"/>
        <Setter Property="Content" Value="{Binding}"/>
        <Setter Property="ContentTemplate" 
                Value="{StaticResource TabItemContentTemplate}"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="HeaderTemplate" Value="{StaticResource TabItemHeaderTemplateSelected}" />
            </Trigger>
            <Trigger Property="IsSelected" Value="false">
                <Setter Property="HeaderTemplate" Value="{StaticResource TabItemHeaderTemplate}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

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

    <StackPanel Grid.Row="0" Grid.Column="0" Margin="0 20 0 0">
        <StackPanel Orientation="Horizontal">
            <Label Content="Model Name:" Margin="0 5 2 0" Width="100" />
            <TextBox Text="{Binding Config.ModelName, Mode=TwoWay}" VerticalAlignment="Top" Background="WhiteSmoke" Foreground="DarkBlue" Width="200" />
        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <Label Content="Model Description:" Margin="0 5 2 0" Width="100" />
            <TextBox Text="{Binding Config.Description, Mode=TwoWay}" VerticalAlignment="Top" Background="WhiteSmoke" Foreground="DarkBlue" Width="600" />
        </StackPanel>
    </StackPanel>

    <TabControl Grid.Row="1" Grid.Column="0" 
                ItemsSource="{Binding Parts}"
                ItemContainerStyle="{StaticResource TabItemContainerStyle}"
                SelectedItem="{Binding SelectedConfigPartViewModel, Mode=TwoWay}"
                Margin="0 40 0 0" />


</Grid>

Was it helpful?

Solution

Instead of :

<Style x:Key="TabItemContainerStyle" TargetType="TabItem" >
....
....
</Style>

Try below code :

<Style x:Key="TabItemContainerStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}" >
....
....
</Style>

Or if it does not work, then

Change

BasedOn="{StaticResource {x:Type TabItem}}"

to

BasedOn="{StaticResource NameOfYourStyleDeclaredInTheme}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top