XAML 触发器 WPF 选项卡选择的变化多于选项卡标题

发布于 2024-10-26 01:28:58 字数 1877 浏览 2 评论 0原文

我有一个触发器,当选择一个选项卡时,我将选项卡颜色设置为蓝色,将文本颜色设置为白色,但问题是,由于某种原因,该触发器还将选项卡标题正文中的文本颜色以及组框中的文本颜色更改为白色。似乎任何有标题的东西都会变成白色。

如何才能做到只有选项卡本身的标题

<TabItem Header="Query Editor" <-- This text only

在选择时才会更改为白色,而表单上没有其他文本?

下面是我正在使用的代码。

谢谢。

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid>
                    <Border Name="Border"
                        Background="WhiteSmoke"
                        BorderBrush="Black"
                        BorderThickness="1,1,1,1"
                        CornerRadius="6,6,0,0">

                        <ContentPresenter x:Name="ContentSite"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            ContentSource="Header"
                            Margin="12,2,12,2"/>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="#00396a" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="WhiteSmoke" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I have a trigger that when a tab is selected I color the tab blue and the text white, but the problem is that this trigger, for some reason, also changes the text color in the tabheader body to white as well in the group boxes. It seems that anything that has a header turns to white.

How do I make it so only the header in the tab itself

<TabItem Header="Query Editor" <-- This text only

will change to white when selected and no other text on the form?

Below is the code I am using.

Thanks.

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid>
                    <Border Name="Border"
                        Background="WhiteSmoke"
                        BorderBrush="Black"
                        BorderThickness="1,1,1,1"
                        CornerRadius="6,6,0,0">

                        <ContentPresenter x:Name="ContentSite"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            ContentSource="Header"
                            Margin="12,2,12,2"/>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="#00396a" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="WhiteSmoke" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

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

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

发布评论

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

评论(2

梦在深巷 2024-11-02 01:28:58

也许 TabItem.HeaderTemplate 适合您。

        <TabControl>
            <TabControl.Resources>
                 <DataTemplate x:Key="headerText">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="content" Text="{Binding}" Grid.Column="1"/>
                </Grid>
                <DataTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.Setters>
                            <Setter TargetName="content" Property="Foreground" Value="white" />
                        </Trigger.Setters>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
            <Style TargetType="TabItem">
                <Style.Setters>
                    <Setter Property="HeaderTemplate" Value="{StaticResource headerText}" />
                </Style.Setters>
            </Style>
            </TabControl.Resources>



            <TabItem Header="fsdf" >
                <TextBlock Text="lkajsldkjaskl" />
            </TabItem>
            <TabItem Header="ghj">
                <TextBlock Text="ghj" />
            </TabItem>
            <TabItem Header="fs556df">
                <TextBlock Text="jjj" />
            </TabItem>
        </TabControl>

    </TabItem>
</TabControl>

你可以有 2headerstyle,选择一个普通的。在 tabitem 样式触发器中,您可以将 headertemplate 设置为右侧的“IsSelected”模板。

may be TabItem.HeaderTemplate will work for you.

        <TabControl>
            <TabControl.Resources>
                 <DataTemplate x:Key="headerText">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="content" Text="{Binding}" Grid.Column="1"/>
                </Grid>
                <DataTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.Setters>
                            <Setter TargetName="content" Property="Foreground" Value="white" />
                        </Trigger.Setters>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
            <Style TargetType="TabItem">
                <Style.Setters>
                    <Setter Property="HeaderTemplate" Value="{StaticResource headerText}" />
                </Style.Setters>
            </Style>
            </TabControl.Resources>



            <TabItem Header="fsdf" >
                <TextBlock Text="lkajsldkjaskl" />
            </TabItem>
            <TabItem Header="ghj">
                <TextBlock Text="ghj" />
            </TabItem>
            <TabItem Header="fs556df">
                <TextBlock Text="jjj" />
            </TabItem>
        </TabControl>

    </TabItem>
</TabControl>

you could have 2headerstyle, one normal one selected. in your tabitem style triggers you can set the headertemplate to the right "IsSelected" one.

凉风有信 2024-11-02 01:28:58

解决方案!

谢谢大家的帮助。

我所做的更改是删除了 ContentPresenter 并将其替换为仅影响选项卡标题本身的文本块。

<TabControl.Resources>
    <Style TargetType="{x:Type TabControl}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush>
                    <GradientStop Offset="0" Color="WhiteSmoke"/>
                    <GradientStop Offset="4" Color="GhostWhite"/>
                 </LinearGradientBrush>
             </Setter.Value>
         </Setter>
         <Setter Property="Foreground" Value="Black" />
     </Style>

     <Style TargetType="{x:Type TabItem}">
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="{x:Type TabItem}">
                     <Grid>
                         <Border Name="Border"
                             Background="WhiteSmoke"
                             BorderBrush="Black"
                             BorderThickness="1,1,1,1"
                             CornerRadius="6,6,0,0">

                             <TextBlock 
                                 x:Name="TabItemContent" 
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center"
                                 Text="{TemplateBinding Header}"
                                 Margin="12,2,12,2" />
                         </Border>
                     </Grid>
                     <ControlTemplate.Triggers>
                         <Trigger Property="IsSelected" Value="True">
                             <Setter TargetName="Border" Property="Background" Value="#00396a" />
                             <Setter Property="Foreground" TargetName="TabItemContent" Value="White"/>
                         </Trigger>
                         <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="WhiteSmoke" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.Resources>

The solution!

Thank you all for your help.

What I changed was I removed the ContentPresenter and replaced it with the textblock that only affected the tab header itself.

<TabControl.Resources>
    <Style TargetType="{x:Type TabControl}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush>
                    <GradientStop Offset="0" Color="WhiteSmoke"/>
                    <GradientStop Offset="4" Color="GhostWhite"/>
                 </LinearGradientBrush>
             </Setter.Value>
         </Setter>
         <Setter Property="Foreground" Value="Black" />
     </Style>

     <Style TargetType="{x:Type TabItem}">
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="{x:Type TabItem}">
                     <Grid>
                         <Border Name="Border"
                             Background="WhiteSmoke"
                             BorderBrush="Black"
                             BorderThickness="1,1,1,1"
                             CornerRadius="6,6,0,0">

                             <TextBlock 
                                 x:Name="TabItemContent" 
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center"
                                 Text="{TemplateBinding Header}"
                                 Margin="12,2,12,2" />
                         </Border>
                     </Grid>
                     <ControlTemplate.Triggers>
                         <Trigger Property="IsSelected" Value="True">
                             <Setter TargetName="Border" Property="Background" Value="#00396a" />
                             <Setter Property="Foreground" TargetName="TabItemContent" Value="White"/>
                         </Trigger>
                         <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="WhiteSmoke" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文