I have a section header which contains two words to be displayed in XAML with a little bit of style with fontsize and fontfamily. I can implement this with TextBlock or ContentControl (by setting the content property to the header text).

The TextBlock is not derived from Control, so is the intent of TextBlock to perform better in XAML for text compared to a contentcontrol?

有帮助吗?

解决方案

Your TextBlock is just that, a TextBlock as framework element when you invoke it is just an object. So when you write <TextBlock Text="Blah Blah Blah"/> that's literally all it is.

When you're using a ContentControl you're actually invoking a templated control that will have multiple elements in its tree that are added for every instance. So example you're using;

<Style TargetType="ContentControl">
      <Setter Property="Foreground" Value="#FF000000"/>
      <Setter Property="HorizontalContentAlignment" Value="Left"/>
      <Setter Property="VerticalContentAlignment" Value="Top"/>
      <Setter Property="Template">
          <Setter.Value>
              <ControlTemplate TargetType="ContentControl">
                  <ContentPresenter
                      Content="{TemplateBinding Content}"
                      ContentTemplate="{TemplateBinding ContentTemplate}"
                      Cursor="{TemplateBinding Cursor}"
                      Margin="{TemplateBinding Padding}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
              </ControlTemplate>
          </Setter.Value>
      </Setter>
  </Style>

So a short version to your answer would be Yes, TextBlock will perform better and is suggested to be used over templated controls like ContentControl or Label etc. wherever possible.

Hope this helps, cheers.

其他提示

To directly answer your question -- if your intent is to only always render text, yes, use only a TextBlock. As others have pointed out using a ContentControl/ContentPresenter and setting to a string will wrap it anyway, and you have increased your element count +1 unnecessarily.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top