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

发布于 2024-10-09 08:28:44 字数 2903 浏览 6 评论 0 原文

在第二个代码中,有一个文本块,其中包含文本“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:

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爺獨霸怡葒院 2024-10-16 08:28:44

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

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

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.

无声无音无过去 2024-10-16 08:28:44

我通过命名 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>

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.

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>

玉环 2024-10-16 08:28:44

我通过使其成为资源字典来扩展 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>

I extended Scott Solmer's great code by making it a resource dictionary because I needed this TabItem styling application wide.
So add new TabItemStyles.xaml under the Resources Dictionary folder which was called "Resources" :

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

I changed my app.xaml to know about the new resource dictionary. WARNING - adjust the source and component locations for your application:

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

Then I used style in the actual screen everywhere in the application I needed. Unlike Scott, I prefer using StaticResource rather than DynamicResource unless I have to.:

<TabControl>
   <TabItem Header="Tab1" Style="{StaticResource ColoredTabsStyle}">
   <TabItem Header="Tab2" Style="{StaticResource ColoredTabsStyle}">
   <TabItem Header="Tab3" Style="{StaticResource ColoredTabsStyle}">
<TabControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文