如何在不设置 ContentPresenter 样式的情况下设置 TabControl 的 TabItem 样式? [英] How to style a TabControl's TabItem without styling their ContentPresenters?

查看:30
本文介绍了如何在不设置 ContentPresenter 样式的情况下设置 TabControl 的 TabItem 样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TabControl,我对其应用了一个样式,以便 TabItems 的标题没有背景和边框.

I have a TabControl, and I apply a style to it in order for my TabItems' header to have no background nor border.

我的问题是,如果我为选定的触发器添加触发器,相应的面板也会设置样式.因此,如果我选择将所选 TabItem 的前景设置为红色,面板的文本块也会变为红色.

My problem is that if I add a trigger for the selected one, the corresponding panel get styled as well. So if I choose to set the selected TabItem's foreground to Red, the panel's textblocks become red as well.

有趣的事实:它不适用于标签.我很迷茫.

Fun fact: it doesn't apply to Labels. I'm quite lost.

这是 XAML,它在 Kaxaml 中的测试速度非常快:

Here's the XAML, that tests pretty fast in Kaxaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <TabControl>
        <TabControl.Resources>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" Color="Black" BlurRadius="3"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid>
                                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Panel.ZIndex" Value="100" />
                                    <Setter Property="FontWeight" Value="Bold"/>
                                    <Setter Property="Foreground" Value="DarkOrange"/>
                                    <Setter Property="Effect">
                                        <Setter.Value>
                                            <DropShadowEffect ShadowDepth="0" BlurRadius="4" Color="Blue"/>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Foreground" Value="#FF553333" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

            </Style>
        </TabControl.Resources>
        <TabItem Header="TAB1">
            <TextBlock Text="WHY AM I GETTING STYLED D:"/>
        </TabItem>
        <TabItem Header="TAB2">
          <TextBlock Text="ME TOO D:"/>
        </TabItem>
    </TabControl>
    </Grid>
</Window>

<小时>

回答后编辑(以防有人对这个非常漂亮的 TabControl 感兴趣...):


Edit after answer (in case anyone's interested in this oh-so-pretty TabControl...):

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TabControl>
        <TabControl.Resources>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" Color="Black" BlurRadius="3"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Border BorderBrush="Transparent" Background="Transparent">
                                    <ContentPresenter x:Name="TabItemContent" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>                                    
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Panel.ZIndex" Value="100" TargetName="TabItemContent"/>
                                    <Setter Property="TextElement.FontWeight" Value="Bold" TargetName="TabItemContent"/>
                                    <Setter Property="TextElement.Foreground" Value="DarkOrange" TargetName="TabItemContent"/>
                                    <Setter Property="Effect" TargetName="TabItemContent">
                                        <Setter.Value>
                                            <DropShadowEffect ShadowDepth="0" BlurRadius="4" Color="Blue"/>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" Value="Black" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

            </Style>
        </TabControl.Resources>
        <TabItem Header="TAB1">
          <StackPanel>
           <TextBlock Text="TextBlock"/>
           <Label Content="Label"/>
          </StackPanel>

        </TabItem>
        <TabItem Header="TAB2">
            <StackPanel>
           <TextBlock Text="TextBlock too"/>
           <Label Content="Another Label"/>
          </StackPanel>
        </TabItem>
    </TabControl>
</Grid>
</Window>

推荐答案

我对您的代码进行了更改!!现在好了!检查一下

I made a change in your code!! Its fine now! check it

<Window x:Class="WpfApplication1.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">
    <Grid>
        <TabControl>
            <TabControl.Resources>
                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect ShadowDepth="0" Color="Black" BlurRadius="3"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>

                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TabItem}">
                                <Grid>                                    
                                        <ContentPresenter x:Name="TabItemContent" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>                                    
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Panel.ZIndex" Value="100" />
                                        <Setter Property="FontWeight" Value="Bold"/>
                                        <Setter Property="TextElement.Foreground" TargetName="TabItemContent" Value="DarkOrange"/>
                                        <Setter Property="Effect">
                                            <Setter.Value>
                                                <DropShadowEffect ShadowDepth="0" BlurRadius="4" Color="Blue"/>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                    <Trigger Property="IsEnabled" Value="False">
                                        <Setter Property="TextElement.Foreground" Value="Black" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>

                </Style>
            </TabControl.Resources>
            <TabItem Header="TAB1">
                <TextBlock Text="Now, I am perfect:"/>
            </TabItem>
            <TabItem Header="TAB2">
                <TextBlock Text="ME TOO D:"/>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

这篇关于如何在不设置 ContentPresenter 样式的情况下设置 TabControl 的 TabItem 样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆