Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a style to TextBlocks within a ContentPresenter in Silverlight

If I have the following style defined:

<UserControl.Resources>
    <Style TargetType="TextBlock" x:Key="ProblemStyle">
        <Setter Property="FontSize" Value="40"/>
        <Setter Property="FontWeight" Value="Bold"/>
    </Style>
</UserControl.Resources>

Then when I have a ContentPresenter data bound to a string, in WPF I can get it to style the text as required with the following XAML:

<ContentPresenter Content="{Binding Problem}">
    <ContentPresenter.Resources>
        <Style TargetType="TextBlock" BasedOn="{StaticResource ProblemStyle}" />
    </ContentPresenter.Resources>
</ContentPresenter>

However, in Silverlight, this doesn't work. Is there a way that works for both?

like image 772
Mark Heath Avatar asked Sep 16 '10 15:09

Mark Heath


1 Answers

Use the TextElement Attached property. You will not be able to set a style, but most of the properties that effects the Textblock are there..

<ContentPresenter x:Name="ContentPresenter"
                              ContentSource="Header"
                              HorizontalAlignment="Left"
                              TextElement.FontFamily="Segoe UI"
                              TextElement.FontSize="12"
                              TextElement.FontWeight="Bold"
                              TextElement.Foreground="White"
                              RecognizesAccessKey="True" />
like image 136
OliverAssad Avatar answered Sep 28 '22 00:09

OliverAssad