user2893939
user2893939

Reputation: 31

WPF:how to override foreground property of header cell for globally defined text block in XAML

Globally defined Text Block of Gray color

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="{StaticResource BR_SE_Gray}" />
    <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
    <Setter Property="FontSize" Value="11"/>
    <Setter Property="FontFamily" Value="Arial Unicode MS"/>
    <Style.Triggers> 
        <Trigger Property="controls:TextBlockService.IsTextTrimmed" Value="True">
            <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
        </Trigger>
    </Style.Triggers>

I want Black color in Grid View Header Cell in the below mentioned code. I want black color in foreground property in the above code block. The globally defined text block color is grey. How to override that and make it black in the below code

<ContentControl x:Name="ContentPresenter"
    Grid.Column="0"
    Margin="{TemplateBinding Padding}"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    Content="{TemplateBinding Content}"
    ContentTemplate="{TemplateBinding ContentTemplate}"
    FontFamily="pack://application:,,,/Themes/#Arial Rounded MT Bold"
    FontWeight="Bold"
    Foreground="{TemplateBinding Foreground}"
    IsTabStop="{TemplateBinding IsTabStop}">

    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Setter Property="FontSize" Value="8" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontFamily" Value="pack://application:,,,/Themes/#Arial Rounded MT Bold" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </ContentControl.Style>

Upvotes: 1

Views: 569

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

Resources are resolved by traversing up logical tree. So, you can override resource by specifying resource for textBlock under resource section of ContentControl and set Foreground to Black there.

This way, new style will get applied to all the textBlocks falling under ContentControl and textBoxes falling outside of ContentControl will continue using your global style.

Make sure you set BasedOn for Style so that your style inherit other properties set in base style -

<ContentControl>
   <ContentControl.Resources>
      <Style TargetType="{x:Type TextBlock}"
             BasedOn="{StaticResource {x:Type TextBlock}}">
          <Setter Property="Foreground" Value="Black" />
      </Style>
   </ContentControl.Resources>
</ContentControl>

Upvotes: 1

Related Questions