无法在WPF中的TabControl模板中设置触发器

时间:2015-06-03 13:16:15

标签: wpf xaml triggers tabcontrol tabitem

我为 TabControl 写了一个样式。在TabControl中我有 TextBlock 按钮。我希望为TabItem.IsSelected设置触发器,以便TextBlock中文本的字体颜色发生变化。我的代码不起作用:

  <Style x:Key="_tabItemButtonStyle" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type TabItem}">
      <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Name="_TabHeaderStackPanel" >
                            <TextBlock Text="{ Binding TabName }" Name="_TabHeaderText" Background="{ Binding TabBackColour }" FontSize="{ Binding TabFontSize }" >
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected,
                                    RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                            Value="True">
                                                <Setter Property="Foreground" Value="SteelBlue"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>

                            </TextBlock>
       ...

我怀疑的问题是这段代码:

                                 <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected,
                                    RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                            Value="True">
                                                <Setter Property="Foreground" Value="SteelBlue"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

修改

我将标签项绑定到ViewModel集合。所以我的样式绑定看起来如下:

 <TabControl x:Name="_MainTabControl"  Grid.Column="1" Grid.Row="1"
                    SelectedIndex="0"
                    ItemsSource="{Binding OpenTabs}"
                    ItemContainerStyle="{StaticResource _tabItemButtonStyle}" />

1 个答案:

答案 0 :(得分:0)

我不知道MetroTabItem是什么。但其他代码看起来很好。 我用示例代码试了一下。它完全符合您所描述的想要实现的目标。

请检查TabItem上的Style = {StaticResource YourStyleName}是否正确应用了样式

这是我的XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Grid>
    <Grid.Resources>
        <ResourceDictionary>
    <Style TargetType="{x:Type TabItem}" x:Key="TestStyle">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Name="_TabHeaderStackPanel" >
                        <TextBlock Text="test" Name="_TabHeaderText" Background="{ Binding TabBackColour }" FontSize="{ Binding TabFontSize }" >
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsSelected,
                                RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                        Value="True">
                                            <Setter Property="Foreground" Value="SteelBlue"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>

                        </TextBlock>
                        <Button Content="Test"></Button>
                        </StackPanel></DataTemplate></Setter.Value>

                    </Setter>
            </Style>
        </ResourceDictionary>
    </Grid.Resources>
    <TabControl>
        <TabItem Style="{StaticResource TestStyle}">
        </TabItem>
        <TabItem Style="{StaticResource TestStyle}">

        </TabItem>
    </TabControl>
    </Grid>