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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4532579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:26:35  来源:igfitidea点击:

Changing the text color of the selected tabItem in a TabControl?

wpftabcontroltabitem

提问by hafhadg3

In the second code there is a textBlock with the text "Mina ?vningar" How can I change the text color to black when the tabItem is selected?

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

style:

风格:

 <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

替代文字

回答by Rick Sladkey

One solution is to use a separate style for this situation:

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

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

and then replace:

然后替换:

Foreground="White" 

with:

和:

Style="{StaticResource TabItemText}"

in the TextBlock.

在文本块中。

回答by Scott Solmer

I did this by naming the ContentPresenter and targeting it in the trigger. This way it keeps everything for the TabItem style in one place.

我通过命名 ContentPresenter 并在触发器中定位它来做到这一点。这样,它将 TabItem 样式的所有内容都保存在一个地方。

Complete Example:

完整示例:

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>