如何在不设置 ContentPresenters 样式的情况下设置 TabControl 的 TabItem 样式?
How to style a TabControl's TabItem without styling their ContentPresenters?
我有一个 TabControl,我对其应用了一种样式,以使我的 TabItems header 没有背景也没有边框。
我的问题是,如果我为选定的触发器添加触发器,相应的面板也会设置样式。因此,如果我选择将所选 TabItem 的前景设置为红色,面板的文本块也会变成红色。
有趣的事实:它不适用于标签。我迷路了。
这是 XAML,在 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>
回答后编辑(如果有人对此感兴趣 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>
我修改了你的代码!!现在好了!检查一下
<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 试试这个小修改:
<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="MyTarget" 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" Value="DarkOrange" TargetName="MyTarget"/>
<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="#FF553333" TargetName="MyTarget"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有一个 TabControl,我对其应用了一种样式,以使我的 TabItems header 没有背景也没有边框。
我的问题是,如果我为选定的触发器添加触发器,相应的面板也会设置样式。因此,如果我选择将所选 TabItem 的前景设置为红色,面板的文本块也会变成红色。
有趣的事实:它不适用于标签。我迷路了。
这是 XAML,在 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>
回答后编辑(如果有人对此感兴趣 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>
我修改了你的代码!!现在好了!检查一下
<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 试试这个小修改:
<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="MyTarget" 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" Value="DarkOrange" TargetName="MyTarget"/>
<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="#FF553333" TargetName="MyTarget"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>