Applying Style to TextBlocks in ContentPresenter in Silverlight

If I have the following style:

<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 ContentPresenter data bound to a string, in WPF I can force it to style the text as needed with the following XAML:

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

However, this does not work in Silverlight. Is there a way that works for both?

+3
source share
2 answers

Use the TextElement Attached property. You cannot set the style, but most of the properties that affect Textblock are.

<ContentPresenter x:Name="ContentPresenter"
                              ContentSource="Header"
                              HorizontalAlignment="Left"
                              TextElement.FontFamily="Segoe UI"
                              TextElement.FontSize="12"
                              TextElement.FontWeight="Bold"
                              TextElement.Foreground="White"
                              RecognizesAccessKey="True" />
+5
source

-: , "ProblemStyle" , ContentPresenter. Silverlight , , , , .

, , , ContentControl - ContentPresenter .

ContentControl?

<Style x:key="ProblemStyle" TargetType="ContentControl">
  <Setter Property="FontSize" Value="40"/>
  <Setter Property="FontWeight" Value="Bold"/>
</Style>

ContentControl StaticResource "ProblemStyle".

ContentControl ContentPresenter - ContentPresenter :

<Style x:key="ProblemStyle" TargetType="ContentControl">
  <Setter Property="FontSize" Value="40"/>
  <Setter Property="FontWeight" Value="Bold"/>
  <Setter Property="Template">
    <Setter.Value>
       <ControlTemplate TargetType="ContentControl">
          <Border>
             <ContentPresenter Content="{TemplateBinding Content}"/>
          </Border>
       </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

, , / .

-1

Source: https://habr.com/ru/post/1765130/


All Articles