我在xaml文件中为Tab Control和TabItem创建了样式。
当TabControl第一次加载到窗口时,它的应用样式就像它想要的那样。 但是当我选择另一个tabItem时,它没有执行IsSelection属性样式。
选择 :Path Stroke =" blue"和背景Bd ="黄色"。
请帮我解决这个问题。
提前谢谢。
<Style x:Key="SimpleTabItemStyle" TargetType="{x:Type TabControl}">
<Setter Property="Padding" Value="4,4,4,0"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{DynamicResource WhiteColorBrush}"/>
<Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnDefinition0" Width="480"/>
<ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition x:Name="RowDefinition0" Height="30"/>
<RowDefinition x:Name="RowDefinition1" Height="*"/>
</Grid.RowDefinitions>
<TabPanel x:Name="HeaderPanel" IsItemsHost="true" Margin="2,2,2,0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
<Border x:Name="ContentPanel"
CornerRadius="0,5,5,5"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{DynamicResource NormalBrush}" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
<ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource WhiteColorBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MediumTabItemStyle" TargetType="{x:Type TabItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="3,1"/>
<Setter Property="Focusable" Value="True"/>
<Setter Property="Width" Value="105px"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}" >
<Grid SnapsToDevicePixels="true" Height="32" Focusable="False">
<Path x:Name="Bd" Fill="{DynamicResource NormalBrush}" StrokeThickness="2" Stroke="Blue" Data="M32.112576,21.370581 C32.11147,19.654586 32.018481,10.83178 32.130291,
2.8647957 32.168301,0.15634975 32.890504,-0.063229885 33.673263,-0.10401306 33.673263,-0.10401306 46.603605,-0.10439528 46.603605,-0.10439528 48.464699,-0.081679943 48.572192,13.549447 50.678186,21.503142" Stretch="Fill" Width="105px" Cursor="Hand" />
<TextBlock x:Name="Content" Padding="1" TextWrapping="WrapWithOverflow" Width="95" Foreground="{DynamicResource BlackColorBrush}" HorizontalAlignment="Left" TextBlock.TextAlignment="Center" VerticalAlignment="center"
Text="{Binding Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" ToolTip="{Binding Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" TextTrimming="CharacterEllipsis"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="False">
<Setter Property="ToolTip" TargetName="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem},Mode=FindAncestor},Path=Header}"/>
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedBrush}"/>
<!--<Setter Property="TextTrimming" TargetName="Content" Value="CharacterEllipsis"/>-->
<Setter Property="Panel.ZIndex" Value="0"/>
<Setter Property="Stroke" TargetName="Bd" Value="{DynamicResource WhiteColorBrush}"/>
<Setter Property="Margin" Value="0,0,0,-1" TargetName="Bd"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Bd" Property="Opacity" Value="0.6"/>
<Setter Property="Cursor" TargetName="Bd" Value="Hand" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource NormalBrush}"/>
<Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>
<Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
<!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>-->
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="false"/>
<Condition Property="IsMouseOver" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="Margin" Value="-1" TargetName="Bd"/>
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedItemBrush}"/>
<Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
<!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>-->
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource DisabledBackgroundBrush}"/>
<Setter Property="Foreground" TargetName="Content" Value="{DynamicResource WhiteColorBrush}"/>
<Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
<Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource NormalBrush}"/>
<Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>
<Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
请尝试以下代码:
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="False">
<Setter Property="ToolTip" TargetName="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem},Mode=FindAncestor},Path=Header}"/>
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedBrush}"/>
<!--<Setter Property="TextTrimming" TargetName="Content" Value="CharacterEllipsis"/>-->
<Setter Property="Panel.ZIndex" Value="0"/>
<Setter Property="Stroke" TargetName="Bd" Value="{DynamicResource WhiteColorBrush}"/>
<Setter Property="Margin" Value="0,0,0,-1" TargetName="Bd"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<!--<Setter TargetName="Bd" Property="Opacity" Value="0.6"/>-->
<Setter Property="Cursor" TargetName="Bd" Value="Hand" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
<Setter Property="Fill" TargetName="Bd" Value="Blue"/>
<Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>
<Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
<!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>-->
</Trigger>
<!--<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="false"/>
<Condition Property="IsMouseOver" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="Margin" Value="-1" TargetName="Bd"/>
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedItemBrush}"/>
<Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
--><!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>--><!--
</MultiTrigger>-->
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource DisabledBackgroundBrush}"/>
<Setter Property="Foreground" TargetName="Content" Value="{DynamicResource WhiteColorBrush}"/>
<Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
<Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
<!--<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource NormalBrush}"/>
<Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>-->
<Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>