locked
Textbloxk style override label style in WPF RRS feed

  • Question

  • Hello.

    I create 2 resource dictionaries one for TextBlock and other for labels:

    //for labels

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">      

        <Style BasedOn="{x:Null}" TargetType="{x:Type Label}"

            <Setter Property="Foreground" Value="Blue"/>

        </Style>

    </ResourceDictionary>

    //for TextBlock

    <ResourceDictionary

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <Style BasedOn="{x:Null}" TargetType="{x:Type TextBlock}">

            <Setter Property="Foreground" Value="Red"/>

        </Style>   

    </ResourceDictionary>

    The only differences is the color of foreground one is red and the other y blue.

    In both case the style doesn´t has key name show this style will be use for all the labels and textblock in my application. For do this I insert in a MergedDictionaries in my app.xaml

    <Application.Resources>

          <ResourceDictionary>

                <ResourceDictionary.MergedDictionaries>               

                    <ResourceDictionary Source="/WpfControlLibrary1;componentLabelResourceDictionary.xaml"/>  

                    <ResourceDictionary Source="/WpfControlLibrary1;component/ TextblockResourceDictionary.xaml"/>                 

    </ResourceDictionary.MergedDictionaries>

          </ResourceDictionary>

    </Application.Resources>

     When I put in my application one label and one TextBlock, both set the color red.

    I know that a label has inside a TextBlock, but I don´t know how to set the color of my label in a different color of the text block.

    Not only the label is override by TextBlock, all the controls that has a TextBlock inside will have the same color, like a button.

    I can´t use a x:key for TextBlock because this is an application that has a lot of TextBlock, and is not possible to set a key for all.

    Any way to change the color of my label??.

     

    Thursday, August 19, 2010 8:22 AM

Answers

  • Hi Imakil,

    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 Appliacton level resource. And if you difine a style for TextBlock at Appliaction level, then it will be applied to these TextBlock within ControlControls.

    You can find details about the display logic of ContentPresenter at http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter.aspx.

    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.

    Here is an example:

    <ResourceDictionary 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:system="clr-namespace:System;assembly=mscorlib">
    
      <DataTemplate DataType="{x:Type system:String}">
        <TextBlock Text="{Binding}">
          <TextBlock.Resources>
            <Style TargetType="{x:Type TextBlock}"/>
          </TextBlock.Resources>
        </TextBlock>
      </DataTemplate>
    
      <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red"/>
      </Style>
    </ResourceDictionary>
    

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Thursday, August 26, 2010 1:23 AM
    Monday, August 23, 2010 7:52 AM

All replies

  • Hi,

    Try out this

     <Style BasedOn="{x:Null}" TargetType="{x:Type Label}">
    
          <Setter Property="TextBlock.Foreground" Value="Blue"></Setter>
    
        </Style>

    i hope this will help you.

    mark it as answer if it realy helps you

    Thanks,

    BHavik

    Thursday, August 19, 2010 8:35 AM
  • hi there,

    Define the Key for that style and then give it to that Textblock then it will not be ovverriden in the Label,

    Label in it also uses textBlock i think,

    regards,

     

    Thursday, August 19, 2010 10:01 AM
  • thanks.

    I try to set the property of TextBlock.Foreground but the color doesn´t change.

    any other idea

     

     

    Thursday, August 19, 2010 11:51 AM
  • This is the way to doit, but my problem is that i have an application with a lot of textblock, that doesn´t have Key, and if i do this I need to change all of them.

    Also the Label use a TextBlock in it,  so problem is how to set the textblock inside the label to other style, not to default, or to change the properties of the text block.

    I try with  <Setter Property="TextBlock.Foreground" Value="Blue"></Setter> but nothing change

    any idea to solve the problem?

    Thursday, August 19, 2010 11:55 AM
  • Hi Imakil,

    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 Appliacton level resource. And if you difine a style for TextBlock at Appliaction level, then it will be applied to these TextBlock within ControlControls.

    You can find details about the display logic of ContentPresenter at http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter.aspx.

    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.

    Here is an example:

    <ResourceDictionary 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:system="clr-namespace:System;assembly=mscorlib">
    
      <DataTemplate DataType="{x:Type system:String}">
        <TextBlock Text="{Binding}">
          <TextBlock.Resources>
            <Style TargetType="{x:Type TextBlock}"/>
          </TextBlock.Resources>
        </TextBlock>
      </DataTemplate>
    
      <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red"/>
      </Style>
    </ResourceDictionary>
    

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Thursday, August 26, 2010 1:23 AM
    Monday, August 23, 2010 7:52 AM