8

We are using global styles definitions for most of the types. We define then in the app.xaml file. When using TextBlock it is a problem to define a foreground color because it changes all the controls using TextBlock (Button's content color for example). How can we define a global style which will act only on specific TextBlock usages?

current problematic usage:

<Style TargetType={x:Type TextBlock}>
  <Setter Property="Foreground" Value="Red"/>
</Style>
0

3 Answers 3

2

Since I don't think there is a way to differentiate “your” TextBlocks and those that are part of other controls, your options are quite limited.

  • You could create named Style and add Style="{StaticResource coloredTextBlock}" or Foreground="{StaticResource textBlockColor}" to all TextBlocks. This would be quite tedious and non-DRY.
  • You could create your own type that inherits from TextBlock and style that. This has some of the disadvantages of the above solution (you have to remember doing that). But it has much less repetition.
2

This is because ContentPresenter creates a TextBlock for a string content, and since that TextBlock isn't in the visual tree, it will lookup to Application level resource. And if you define a style for TextBlock at Application level, then it will be applied to these TextBlock within ControlControls.

A workaround is to define a DataTemplate for System.String, where we can explicitly use a default TextBlock to display the content. You can place that DataTemplate in the same dictionary you define the TextBlock style so that this DataTemplate will be applied to whatever ContentPresenter effected by your style.

Add this to your Application resources and it should work for you -

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
    <TextBlock.Resources>
      <Style TargetType="{x:Type TextBlock}"/>
    </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

Declare a namespace in your xaml, if not referred already -

xmlns:system="clr-namespace:System;assembly=mscorlib"

EDIT : Check this sample where its working..

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Foreground" Value="Red"/>
</Style>

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
     <TextBlock.Resources>
        <Style TargetType="{x:Type TextBlock}"/>
     </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

<Style TargetType="{x:Type Button}">
  <Setter Property="Foreground" Value="Yellow"/>
</Style>

<Style TargetType="{x:Type Label}">
  <Setter Property="Foreground" Value="Blue"/>
</Style>
5
  • Thanks. what about other controls that use TextBlock in there style (explicit)?
    – ittay
    Oct 25, 2011 at 9:09
  • BTW I have tried to add specific UserControl style with TextBlock.Foreground="Red" but had no impact. Any ideas?
    – ittay
    Oct 25, 2011 at 9:13
  • I have tested with small application with DataTemplate i just defined. Isn't this working for you??
    – Rohit Vats
    Oct 25, 2011 at 11:52
  • I have updated my answer. Have a look. In this sample, all your buttons will have foreground color Yellow, all your labels will have foreground color Blue and your Textblock will have foreground color Red.
    – Rohit Vats
    Oct 25, 2011 at 12:05
  • Since you are overriding your styles for Button and label by explicitly defining it. So, it will work. :) Moreover, if other controls that use Textblock, if no style is defined at local level ie. at UserControl level, then in that case it will look upto Application level resource and will apply that style.
    – Rohit Vats
    Oct 25, 2011 at 12:17
1

Just provide a x:key in the style, like:

<Style x:Key="stRedTextBlock" TargetType={x:Type TextBlock}>
        <Setter Property="Foreground" Value="Red"/>
</Style>

and mention the key in the TextBlock control style, where ever you require this particular TextBlock style, like:

<TextBlock Name="textBlock1" Style="{StaticResource stRedTextBlock}" />
1
  • This way is tedious because I will need to add each element the style. I want a global style which will work without specific style in each textblock element
    – ittay
    Oct 24, 2011 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.