WPF app使用ExpressionDark主题,想要扩展TabItem Header样式

时间:2014-03-30 17:49:19

标签: wpf xaml themes datatemplate tabcontrol

对于使用ExpressionDark主题的WPF应用程序,我有一个TabControl,没有任何修改,选项卡控件的主题样式很好。

但是,当我通过样式设置器实现HeaderTemplate时,这似乎会覆盖标题模板的主题样式。

我想要做的是扩展主题的样式,以便我的标签控件的标题可以包含一个图像(在下面的示例代码中,它是硬编码的,但最终会被绑定和动态)。

有没有办法指示 TabItemHeaderTemplate TabItemHeaderTemplateSelected 项目以某种方式从主题模板中做一个BasedOn?

<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>

1 个答案:

答案 0 :(得分:1)

而不是:

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

尝试以下代码:

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

如果它不起作用,那么

更改

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

BasedOn="{StaticResource NameOfYourStyleDeclaredInTheme}"