TextBlock和Label都可以显示文本,属于WPF中比较常用的控件。在最初接触WPF时,我经常为如何选择这两个控件感到困惑。随着对WPF深入学习,对这两个控件也有一些了解。今天就说一些我对TextBlock和Label的看法吧。
Label和TextBlock都是System.Windows.Controls命名空间下的类,但二者的父类并不相同。TextBlock继承自System.Windows.FrameworkElement,从这个角度讲,TextBlock不能称之为“控件”(因为它没有继承Control类,关于Control类,我会在WPF
Unleashed第四章为大家介绍),而Label继承自System.Windows.ContentControl。FrameworkElement是非常底层的类,它同时也是ContentControl的父类。所以,Label相对TextBlock更加高级一些,它能够完成TextBlock所无法完成的工作。例如对于Access
key的支持,而且我们可以在Label内可以放置任意对象,而TextBlock只能显示文本。
现在我们从Visual Tree(Luna主题下)的角度看看两者的区别:
Label
TextBlock
从图中可以看出,Label控件由三个元素组成,其最底层的元素就是TextBlock。而TextBlock的Visual Tree只有它本身。所以可以说Label控件包含着TextBlock。
接下来从模板的角度看一下二者的区别。首先是Label的模板:
<Style TargetType="{x:Type Label}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<Style.Resources>
<ResourceDictionary />
</Style.Resources>
<Setter
Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static
SystemColors.ControlTextBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="Panel.Background">
<Setter.Value>
<SolidColorBrush>
#00FFFFFF</SolidColorBrush>
</Setter.Value>
</Setter>
<Setter Property="Control.Padding">
<Setter.Value>
<Thickness>
5,5,5,5</Thickness>
</Setter.Value>
</Setter>
<Setter
Property="Control.HorizontalContentAlignment">
<Setter.Value>
<x:Static Member="HorizontalAlignment.Left" />
</Setter.Value>
</Setter>
<Setter
Property="Control.VerticalContentAlignment">
<Setter.Value>
<x:Static Member="VerticalAlignment.Top" />
</Setter.Value>
</Setter>
<Setter
Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding
Border.BorderBrush}" BorderThickness="{TemplateBinding
Border.BorderThickness}" Background="{TemplateBinding
Panel.Background}" SnapsToDevicePixels="True"
Padding="{TemplateBinding Control.Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding
Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding
Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding
UIElement.SnapsToDevicePixels}"
ContentTemplate="{TemplateBinding
ContentControl.ContentTemplate}"
RecognizesAccessKey="True" Content="{TemplateBinding
ContentControl.Content}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsEnabled">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static
SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>
False</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
接下来是TextBlock的:
<Style
TargetType="{x:Type TextBlock}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style.Resources>
<ResourceDictionary />
</Style.Resources>
<Setter
Property="TextBlock.TextWrapping">
<Setter.Value>
<x:Static Member="TextWrapping.NoWrap" />
</Setter.Value>
</Setter>
<Setter Property="TextBlock.TextTrimming">
<Setter.Value>
<x:Static Member="TextTrimming.None" />
</Setter.Value>
</Setter>
</Style>
从两段代码中可以明显地看出,Label的模板更加复杂,而且TextBlock控件没有ControlTemplate部分,这和之前的Visual
Tree也是相符合的。现在继续ControlTemplate这个话题,Label的ControlTemplate中包含一个属性触发器(关于属性触发器知识,您可以参考我之前的文章),该触发器的含义是:当Label的IsEnabled发生变化时,它的前景色会发生变化,而TextBlock并不具备这个特性。
从以上这些分析中,可以得出这样的结论:TextBlock属于比较底层的控件,因此它的性能要比Label好一些。如果需求只是纯文本的显示,并且不提供Access
key的支持,那么TextBlock是个不错的选择。
最后感谢浏览 :
P
posted on 2007-07-08 11:15
stswordman 阅读(2579)
评论(4) 编辑 收藏 所属分类:
WPF