在TabControl中更改所选tabItem的文本颜色?

时间:2010-12-26 03:03:19

标签: wpf tabcontrol tabitem

在第二个代码中有一个带有文本“Minaövningar”的textBlock 选择tabItem时,如何将文本颜色更改为黑色?

式:

 <Style TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border Name="Border" Background="Transparent" BorderBrush="Transparent"  BorderThickness="0"  Margin="0,0,0,13" CornerRadius="5" >

                            <ContentPresenter x:Name="ContentSite" VerticalAlignment="Top"  HorizontalAlignment="Center" ContentSource="Header" Margin="9"/>

                        </Border>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="Black"/>
                            <Setter TargetName="Border" Property="Background">
                                <Setter.Value>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                            <GradientStop Color="#FFF9F7FD" Offset="0.432" />
                                            <GradientStop Color="#FFECF7FD" Offset="0.433" />
                                        </LinearGradientBrush>
                                </Setter.Value>
                                </Setter>
                            <Setter TargetName="ContentSite" Property="Margin" Value="9,12,9,9" />
                        </Trigger>

                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="Transparent" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

TabItem的:

 <TabItem Background="White">
            <TabItem.Header>
                <StackPanel Orientation="Vertical">
                    <Image Height="32" Source="/Glosboken;component/Images/library bookmarked.png" />
                    <TextBlock Text="Mina övningar" Margin="0,0,0,0" VerticalAlignment="Center" Foreground="White" />
                </StackPanel>
            </TabItem.Header>
            <Grid>
                <ListBox Height="216" Name="bookslist" VerticalAlignment="Top" Background="White" BorderBrush="White" Foreground="White" SelectedIndex="0" Margin="0,0,129,0" />
            </Grid>
        </TabItem>

alt text

3 个答案:

答案 0 :(得分:26)

一种解决方案是在这种情况下使用单独的样式:

<Style x:Key="TabItemText" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="True">
            <Setter Property="Foreground" Value="Black"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="False">
            <Setter Property="Foreground" Value="White"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

然后替换:

Foreground="White" 

使用:

Style="{StaticResource TabItemText}"
TextBlock中的

答案 1 :(得分:2)

我是通过命名ContentPresenter并在触发器中定位它来完成此操作的。 这样它就可以将TabItem样式的所有内容保存在一个地方。

完整示例:

enter image description here

<Window x:Class="TabControlText.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Border x:Name="Border" BorderThickness="1,1,1,0" CornerRadius="5,5,0,0"
                        Padding="25,5,25,5" Margin="0,0,0,0" BorderBrush="Gainsboro">
                        <ContentPresenter x:Name="ContentSite" ContentSource="Header" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="White"/>
                            <Setter TargetName="Border" Property="Background" Value="Black"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="Black"/>
                            <Setter TargetName="Border" Property="Background" Value="White" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <TabControl>
        <TabItem Header="Tab 1" Style="{DynamicResource TabItemStyle1}" />
        <TabItem Header="Tab 2" Style="{DynamicResource TabItemStyle1}" />
        <TabItem Header="Tab 3" Style="{DynamicResource TabItemStyle1}" />
        <TabItem Header="Tab 4" Style="{DynamicResource TabItemStyle1}" />
    </TabControl>
</Grid>

答案 2 :(得分:0)

我扩展了 Scott Solmer 的优秀代码,将其设为资源字典,因为我需要这个 TabItem 样式应用程序。 因此,在名为“Resources”的资源字典文件夹下添加新的 TabItemStyles.xaml :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="ColoredTabsStyle" TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Border x:Name="Border" BorderThickness="1,1,1,0" CornerRadius="5,5,0,0"
                        Padding="25,5,25,5" Margin="0,0,0,0" BorderBrush="Gainsboro">
                    <ContentPresenter x:Name="ContentSite" ContentSource="Header" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="White" />
                        <Setter TargetName="Border" Property="Background" Value="Black" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter Property="TextElement.Foreground" TargetName="ContentSite" Value="Black" />
                        <Setter TargetName="Border" Property="Background" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我更改了我的 app.xaml 以了解新的资源字典。警告 - 调整应用程序的源和组件位置:

<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="pack://application:,,,/TruPredict;component/Resources/TabItemStyles.xaml" />
</ResourceDictionary.MergedDictionaries>

然后我在我需要的应用程序的任何地方都使用了实际屏幕中的样式。与 Scott 不同的是,除非万不得已,我更喜欢使用 StaticResource 而不是 DynamicResource。:

<TabControl>
   <TabItem Header="Tab1" Style="{StaticResource ColoredTabsStyle}">
   <TabItem Header="Tab2" Style="{StaticResource ColoredTabsStyle}">
   <TabItem Header="Tab3" Style="{StaticResource ColoredTabsStyle}">
<TabControl>