Wpf 如何设置TabItem';标题前景是什么?

Wpf 如何设置TabItem';标题前景是什么?,wpf,Wpf,我有一个TabControl,在其他选项卡中我有一个叫做“Errors”。当一个名为“ErrorsExist”的属性设置为true时,我需要它的标题前景变成红色。这是我的密码: <TabControl > <TabControl.Resources> <conv:ErrorsExistToForegroundColorConverter x:Key="ErrorsExistToForegr

我有一个TabControl,在其他选项卡中我有一个叫做“Errors”。当一个名为“ErrorsExist”的属性设置为true时,我需要它的标题前景变成红色。这是我的密码:

           <TabControl >
            <TabControl.Resources>
                <conv:ErrorsExistToForegroundColorConverter x:Key="ErrorsExistToForegroundColorConverter"/>

                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Foreground="{Binding  Path=ErrorsExist, Converter={StaticResource ErrorsExistToForegroundColorConverter}}" Text="{Binding}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>

            <TabItem x:Name="ErrorsTab" Header="Errors">
我有两个问题

首先,这会将所有选项卡标题设置为红色,我只需要对ErrorsTab选项卡执行此操作

其次,它就是不起作用。我的意思是,从未调用转换器的Convert()方法。你能帮我做这个吗


谢谢。

仅将样式指定给要更改的选项卡项,最好使用DataTrigger来完成此简单任务:

<TabItem x:Name="ErrorsTab" Header="Errors">
    <TabItem.Style>
      <Style TargetType="{x:Type TabItem}">
        <Style.Triggers>
          <DataTrigger Binding="{Binding ErrorsExist}" Value="True">
              <Setter Property="Foreground" Value="Red"/>
          </DataTrigger>
         </Style.Triggers>
       </Style>
    </TabItem.Style>
  </TabItem>

编辑:

问题是TabItem标头不继承父TabItem的DataContext。 如果要使其与转换器一起工作,请手动设置TabHeader DataContext:

          <TextBlock DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" 
                     Foreground="{Binding ErrorsExist,Converter={StaticResource ErrorsExistToForegroundColorConverter}}" Text="{Binding}"/>

仅将样式分配给要更改的选项卡项,最好使用DataTrigger执行此简单任务:

<TabItem x:Name="ErrorsTab" Header="Errors">
    <TabItem.Style>
      <Style TargetType="{x:Type TabItem}">
        <Style.Triggers>
          <DataTrigger Binding="{Binding ErrorsExist}" Value="True">
              <Setter Property="Foreground" Value="Red"/>
          </DataTrigger>
         </Style.Triggers>
       </Style>
    </TabItem.Style>
  </TabItem>

编辑:

问题是TabItem标头不继承父TabItem的DataContext。 如果要使其与转换器一起工作,请手动设置TabHeader DataContext:

          <TextBlock DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" 
                     Foreground="{Binding ErrorsExist,Converter={StaticResource ErrorsExistToForegroundColorConverter}}" Text="{Binding}"/>


非常感谢,它成功了。我只是想知道为什么它不能与转换器一起工作…再试一次,我太慢了,粘贴代码时遇到问题…-)非常感谢,它成功了。我只是想知道为什么它不能与转换器一起工作…再试一次,我太慢了,粘贴代码时遇到问题…-)