TextBlock VS ContentPresenter,其中一个比另一个消耗更多

时间:2014-06-09 17:49:18

标签: c# wpf xaml textblock contentpresenter

我有一个WPF应用程序,我使用ControlTemplate使用大量的对象集,这里是代码:

    <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>

使用ContentPresenter或TextBlock时,我的问题更轻,耗费更少?

1 个答案:

答案 0 :(得分:2)

我对Graph#不是很自负,所以这个答案假设Vertex是一个字符串。如果是这种情况,那么TextBlock将是一个更轻量级的控件。

如果将字符串放在内容呈现器中(例如在Label控件中),它将自动在内容呈现器中创建一个TextBlock来托管文本。如果您只是直接使用TextBlock,那么树内的项目就少了一个。