none
TextBlock Foreground Property RRS feed

  • Question

  • Is there any way to change the default TextBlock.Foreground dependency property without affecting other attached properties. For example, I would like to make is so the following code produces Blue text...  

     

    <Grid>
      <Grid.Resources>
       
    <Style TargetType="{x:Type TextBlock}">
         
    <Setter Property="Foreground" Value="Red"/>
       
    </Style>
     
    </Grid.Resources>
     
    <ContentControl TextBlock.Foreground="Blue">
       
    <TextBlock Text="TextBlock"/>
     
    </ContentControl>
    </Grid>

    Thanks in advance. 

    Thursday, December 16, 2010 7:36 PM

Answers

  • Basically the attached property mentioned is for the textblocks contained within the textblock.
    http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.foreground.aspx

    You might have two options for such cases:

    1. Define a local style for the element type:

    <Grid>
      <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
          <Setter Property="Foreground" Value="Red"/>
        </Style>    
      </Grid.Resources>    
      <ContentControl>
        <ContentControl.Resources>
          <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Blue"/>
          </Style>
        </ContentControl.Resources>
        <TextBlock Text="TextBlock"/>
      </ContentControl>    
    </Grid>
    

    2. You can also define a new style based on old style overriding some property values:

    <Grid>
      <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
          <Setter Property="Foreground" Value="Red"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}" x:Key="BlueTextBlock">
          <Setter Property="Foreground" Value="Blue"/>
        </Style>      
      </Grid.Resources>    
      <ContentControl>
        <TextBlock Style="{StaticResource BlueTextBlock}" Text="TextBlock"/> 
      </ContentControl>    
    </Grid>
    

    Thanks,
    Muhammad
    shujaatsiddiqi.blogspot.com


    Muhammad Siddiqi
    Thursday, December 16, 2010 9:49 PM

All replies

  • Hi kolecke,

    Basically WPF has this mechanism for value procedence for dependency properties. You can do it like this if you just want to change the foreground for this text block.

    <Grid>
      <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
          <Setter Property="Foreground" Value="Red"/>
        </Style>
      </Grid.Resources>
      <ContentControl >
        <TextBlock Foreground="Blue" Text="TextBlock"/>
      </ContentControl>
    </Grid>
    

    You can read this about DependencyProperty value prcedence.

    http://msdn.microsoft.com/en-us/library/ms743230.aspx

    Thanks,
    Muhammad
    shujaatsiddiqi.blogspot.com


    Muhammad Siddiqi
    Thursday, December 16, 2010 7:50 PM
  • Unfortunately, it is not always possible to directly set the Foreground property of a TextBlock. For instance, in a ContentPresenter, the TextBlock.Foreground attached property will not work. Is there anyway to set a default Foreground for a TextBlock while allowing attached properties to function?
    Thursday, December 16, 2010 8:55 PM
  • Basically the attached property mentioned is for the textblocks contained within the textblock.
    http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.foreground.aspx

    You might have two options for such cases:

    1. Define a local style for the element type:

    <Grid>
      <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
          <Setter Property="Foreground" Value="Red"/>
        </Style>    
      </Grid.Resources>    
      <ContentControl>
        <ContentControl.Resources>
          <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Blue"/>
          </Style>
        </ContentControl.Resources>
        <TextBlock Text="TextBlock"/>
      </ContentControl>    
    </Grid>
    

    2. You can also define a new style based on old style overriding some property values:

    <Grid>
      <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
          <Setter Property="Foreground" Value="Red"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}" x:Key="BlueTextBlock">
          <Setter Property="Foreground" Value="Blue"/>
        </Style>      
      </Grid.Resources>    
      <ContentControl>
        <TextBlock Style="{StaticResource BlueTextBlock}" Text="TextBlock"/> 
      </ContentControl>    
    </Grid>
    

    Thanks,
    Muhammad
    shujaatsiddiqi.blogspot.com


    Muhammad Siddiqi
    Thursday, December 16, 2010 9:49 PM
  • Hi kolecke,

    I am marking your issue as "Answered", if you have new findings about this issue, please feel free to unmark.

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 27, 2010 11:23 AM
    Moderator