wpf - 更改 TabControl 中选定选项卡项的文本颜色?

标签 wpf tabcontrol tabitem

在第二个代码中有一个文本 block ,其中包含文本“Mina övningar” 选择 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 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

最佳答案

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

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

在文本 block 中。

关于wpf - 更改 TabControl 中选定选项卡项的文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4532579/

相关文章:

WPF DataGrid 有 RowEditEnding 但没有 RowEditEnded

wpf - 在 tabControl 中为 TabItems 使用数据模板

c# - 为 ComboBox 中的 ItemSource 值添加前缀

.net - Silverlight 3 中没有的 WPF 功能列表

WPF 选项卡控件 : Setting Tab Color?

android TabItem onClick 不起作用

wpf - TabControl:所有 TabItem 已折叠,但第一个 TabItem 的内容仍然可见

c# - 运行时新建 TabItem

WPF 数据触发器

asp.net - 在按钮单击中启用和禁用 ajax tabcontainer 中的选项卡