21

在第二个代码中有一个带有文本“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 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>

替代文字

4

3 回答 3

32

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

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

在文本块中。

于 2010-12-26T04:55:13.460 回答
6

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

完整示例:

在此处输入图像描述

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

于 2016-08-04T17:41:15.950 回答
2

我扩展了 Scott Solmer 的出色代码,使其成为资源字典,因为我需要这个 TabItem 样式应用程序。因此,在名为“Resources”的 Resources Dictionary 文件夹下添加新的 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>
于 2021-05-27T14:03:44.600 回答