トリガーを使用して、WPF タブ項目のヘッダー テキスト ブロックの前景色を変更しようとしています。これは、ほとんどの (単純な) シナリオでは問題なく機能しますが、TextBlocks がグローバルにスタイル設定されている場合は機能しません。
したがって、この単純な「マウスオーバー」トリガーは、前景色を変更するという点で機能します。
<Style x:Key="testTabItemStyle1" TargetType="{x:Type TabItem}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Bd" Background="White" BorderBrush="Gray" BorderThickness="1,1,1,0">
<ContentPresenter HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" x:Name="Content" VerticalAlignment="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" ContentSource="Header"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Bd" Value="Black"/>
<Setter Property="Foreground" Value="False"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
問題は、(一貫した外観を維持するために) App.xaml で TextBlocks がグローバルにスタイル設定されている場合、前景は変更されず、グローバルにスタイル設定された前景色が保持されることです。これは、私の TextBlocks がどのようにスタイルされているかです:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="Brown"/>
<Setter Property="Margin" Value="4,0,4,0"/>
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
だから私の質問は、(TabItem のトリガーで) 明示的に定義されたスタイルの割り当てが優先されるべきではないということですか? さらに重要なのは、スタイルをすべてのテキストブロックに個別に割り当てずに、TabItem テキストブロックの色を期待どおりに変更することなく、これを回避するにはどうすればよいですか?
どうもありがとう
NT