Search code examples
c#wpfxamltextblockcontentpresenter

TextBlock VS ContentPresenter, which one consumes more than the other


I have an WPF application, where I use a big number of a object set using a ControlTemplate, here is the code :

    <ControlTemplate x:Name="ControlTemplate" TargetType="{x:Type graphsharp:VertexControl}">
               <Border x:Name="_border" CornerRadius="50" Width="60" Height="60" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                       <Border.Effect>
                            <DropShadowEffect BlurRadius="15" Direction="309" ShadowDepth="0" Color="#FF2B00FF" RenderingBias="Quality"/>
                       </Border.Effect>
                       <Border.Background>
                            <RadialGradientBrush GradientOrigin="0.231,0.889" RadiusX="0.517" RadiusY="0.517">
                                 <GradientStop Color="#CCD4D3FD" Offset="1"/>
                                 <GradientStop Color="Black"/>
                            </RadialGradientBrush>
                       </Border.Background>
                       <Border CornerRadius="50" Width="60" Height="60" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0" BorderThickness="0">
                            <Border.Background>
                                 <RadialGradientBrush RadiusY="0.939" RadiusX="0.982" GradientOrigin="-0.005,0.984" Center="0.417,0.539">
                                      <RadialGradientBrush.RelativeTransform>
                                            <TransformGroup>
                                                 <ScaleTransform CenterY="0.5" CenterX="0.5" ScaleY="1" ScaleX="1"/>
                                                 <SkewTransform AngleY="0" AngleX="0" CenterY="0.5" CenterX="0.5"/>
                                                 <RotateTransform Angle="181.901" CenterY="0.5" CenterX="0.5"/>
                                                 <TranslateTransform/>
                                            </TransformGroup>
                                      </RadialGradientBrush.RelativeTransform>
                                      <GradientStop Color="#34434343" Offset="0.56"/>
                                      <GradientStop Offset="1"/>
                                      <GradientStop Color="#40737373" Offset="0.543"/>
                                      <GradientStop/>
                                 </RadialGradientBrush>
                            </Border.Background>

                            <!--Here is the problem !! -->

                            <ContentPresenter Content="{TemplateBinding Vertex}" 
                                                ContentTemplate="{StaticResource VertexDataTemplate}"  VerticalAlignment="Center" HorizontalAlignment="Center">
                                 <ContentPresenter.Effect>
                                      <DropShadowEffect Color="White" BlurRadius="4" ShadowDepth="0"/>
                                 </ContentPresenter.Effect>
                            </ContentPresenter>

                            <!--Here is the problem !! -->

                      </Border>
                </Border>                        
</ControlTemplate>

My question which is lighter and less consuming to use a ContentPresenter or a TextBlock ?


Solution

  • I'm not au-fait with Graph#, so this answer assumes Vertex is a string. If this is the case then TextBlock will be a more lightweight control.

    If you put a string inside a content presenter (such as in a Label control), it will automatically create a TextBlock inside the content presenter to host the text. If you just use a TextBlock directly then it's one less item inside the tree.