none
Applying style resources to ContentPresenter text RRS feed

  • Question

  • Hi there.

    This should be an easy one, but I am having a hard time styling text rendered in a ContentPresenter.

    I have toyed with:

    Code Snippet

    <ContentPresenter  TextBlock.Foreground="{DynamicResource textWhite}" TextBlock.FontSize="16" >


    and

    Code Snippet

      <ContentPresenter>
                      <ContentPresenter.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                          <Setter Property="FontSize" Value="16"/>
                          <Setter Property="Foreground"  Value="{DynamicResource textWhite}"/>
                        </Style>
                      </ContentPresenter.Resources>
    </ContentPresenter>


    which both kind of work, but seem very clunky. I'd like to re-use my predefined 'H1' resource:

    Code Snippet

     <Style x:Key="H1" TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground"  Value="White"/>
        <Setter Property="FontSize" Value="72"/>
        <Setter Property="FontWeight" Value="Bold"/>
      </Style>


    But there doesn't seem to be anywhere to attach it to, also, as the ContentPresenter can obviously present any type of content, it will not accept a direct style with textblock-defining properties, eg Foreground. Do I have to specify the textblock properties in a datatemplate? Can I apply an existing textblock resource there?

    Is there perhaps a way of telling it to choose a particular style depending on content thrown at it? Eg if text is passed to it, make it 14pt, if its an image, give it a blue border etc?

    Plese bear in mind I do all my work in blend/XAML so no code-behind answers please!

    Any help muchos appreciated

    felix








    Friday, February 1, 2008 11:35 AM

Answers

  • Style can be based on an existing style as follows:

    Code Snippet
    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <
    Page.Resources>
    <
    Style TargetType="{x:Type TextBlock}" x:Key="style">
    <
    Setter Property="Foreground" Value="Red"/>
    <
    Setter Property="FontSize" Value="72"/>
    <
    Setter Property="FontWeight" Value="Bold"/>
    </
    Style>
    </
    Page.Resources>
    <
    ContentPresenter Content="WPF">
    <
    ContentPresenter.Resources>
    <
    Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource style}"/>
    </
    ContentPresenter.Resources>
    </
    ContentPresenter>
    </
    Page>

    Hope this helps
    Monday, February 4, 2008 6:03 AM

All replies

  • Style can be based on an existing style as follows:

    Code Snippet
    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <
    Page.Resources>
    <
    Style TargetType="{x:Type TextBlock}" x:Key="style">
    <
    Setter Property="Foreground" Value="Red"/>
    <
    Setter Property="FontSize" Value="72"/>
    <
    Setter Property="FontWeight" Value="Bold"/>
    </
    Style>
    </
    Page.Resources>
    <
    ContentPresenter Content="WPF">
    <
    ContentPresenter.Resources>
    <
    Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource style}"/>
    </
    ContentPresenter.Resources>
    </
    ContentPresenter>
    </
    Page>

    Hope this helps
    Monday, February 4, 2008 6:03 AM
  • Wow that is perfect thank you! I knew it would be something simple but I couldn't see it in any documentation.

     

    Thanks again!

     

    Monday, February 4, 2008 11:15 AM