如何从TabItem的HeaderTemplate触发'IsSelected'?

时间:2015-05-19 14:02:06

标签: c# wpf triggers tabcontrol

我为自己的WPF应用程序创建了自定义样式TabControl。这包括Style的以下TabItem

<Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
    <Setter Property="Padding" Value="12,2"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Panel.ZIndex" Value="1" />
                        <Setter Property="Foreground" Value="Orange" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

基本上,我希望标题文本的大小为14并以白色着色,除非选择TabItem(然后它应该是橙色): Example of what the TabControl should look like

当我看到TabItem的子控件(未在上面的屏幕中显示)继承FontSizeForeground时,我遇到了问题。在SO快速搜索之后,我来到了这个帖子(SO: How do I set TabControls TabItem header FontSize)并决定使用HeaderTemplate,这是应答者'Novitchi S'的建议。这是我的结果:

<Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
    <Setter Property="Padding" Value="12,2"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate DataType="{x:Type TabItem}">
                <ContentPresenter>
                    <ContentPresenter.Content>
                        <TextBlock x:Name="ContentTextBlock" Foreground="White" FontSize="14" Text="{TemplateBinding Content}"/>
                    </ContentPresenter.Content>
                </ContentPresenter>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=IsSelected}">
                        <Setter TargetName="ContentTextBlock" Property="Foreground" Value="Orange" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Panel.ZIndex" Value="1" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

从积极的方面来说,FontSizeForeground现在正常运作。我唯一无法工作的是在选中标签时为TextBlock橙色着色。我的直觉是DataTrigger无法找到TemplatedParentTextBlock,但我被卡住了。

我有什么选择来实现我的目标?

1 个答案:

答案 0 :(得分:1)

好的问题#1

绑定模式不正确,请勿使用Mode=TemplatedParent,请使用

{ Binding Path=propertyOfTargetElement, 
  RelativeSource={RelativeSource Mode=FindAncestor, 
                  AncestorType={x:Type typeOfTargetElement}
                 }
}

**

**
至于#2问题,我会将触发器移动到文本块中。注意我没有在该datatemplate中看到contentpresenter的用途,因此将其删除,除非出于某种原因存在)。

然后我会做

 <TextBlock>
     <TextBlock.Style>
          <Style.Triggers>
                <DataTrigger