none
How does ContentPresenter set Foreground-Brush in TextBlock? RRS feed

  • Question

  • Hi,

    I would like to understand how it is done that a TextBlock, which is displayed in a ContentPresenter in a template, automatically gets the foreground-brush which is defined in the template.

    I need this information to be able to make a Label, which is displayed in a ContentPresenter, to also get this foreground-brush (I know it´s heavier etc., so no discussion about that).

    I can set TextBlock.Foreground in the ContentPresenter explicitely, but not the needed Control.Foreground, so it seems that the first property is attached to the ContentPresenter anyhow.

    Can anybody explain how the ContentPresenter works internally or at least give me a hint or a link on how I could achive to let the Label use the foreground-color defined by the template?

    Thanks very much in advance,
    Alex
    Tuesday, December 9, 2008 1:06 PM

Answers

  • Foreground is a DependencyProperty, this means that the TextBlock control, not the ContentPresenter, get's the property front its parent all the way to the visual root. What happens with the Label control is that is doesn't necessarily have text for the Content property is of type UIElement so the property has to be set in a different, something like this:

    <ContentControl Foreground="Blue"
       <Label Content="Label Content" Foreground="{Binding RelativeSource={RelativeSource  
          AncestorType={x:Type ContentControl}}, Path=Foreground}"/> 
    </ContentControl> 


    Bigsby, Lisboa, Portugal
    • Marked as answer by Marco Zhou Thursday, December 11, 2008 8:36 AM
    Tuesday, December 9, 2008 2:18 PM

All replies

  • Foreground is a DependencyProperty, this means that the TextBlock control, not the ContentPresenter, get's the property front its parent all the way to the visual root. What happens with the Label control is that is doesn't necessarily have text for the Content property is of type UIElement so the property has to be set in a different, something like this:

    <ContentControl Foreground="Blue"
       <Label Content="Label Content" Foreground="{Binding RelativeSource={RelativeSource  
          AncestorType={x:Type ContentControl}}, Path=Foreground}"/> 
    </ContentControl> 


    Bigsby, Lisboa, Portugal
    • Marked as answer by Marco Zhou Thursday, December 11, 2008 8:36 AM
    Tuesday, December 9, 2008 2:18 PM
  • Foreground is an inheritable property. When set on an element, all the child elements have the same property value set automatically.

    [edit]Sorry didn't notice an answer was already posted.[/edit]
    • Edited by Tim Dawson Tuesday, December 9, 2008 3:07 PM Duplicate
    Tuesday, December 9, 2008 3:06 PM