WPF学习二:TextBlock和Label的区别

TextBlock和Label都是用来显示少许数据的。好多文章对Label存在的描述都是它容许使用"快速获取"。"快速获取"就是容许你用Alt加上其它的按键快速和UI界面的某个控件交互,好比你能够用ALT加上O键来点击一个OK按钮。html

TextBlock直接继承于FrameworkElement,而Label继承于ContentControl。这样看来,Label能够作这样的事情:app

1.能够定义一个控件模板(经过Template属性)性能

2.能够显示出string之外的其余信息(经过Content属性)学习

3.为Label内容添加一个DataItemplate(经过ContentTemplate属性)spa

4.作一些FrameworkElement元素不能作的事情3d

下边是一个TextBlock和Label的继承关系图xml

当Label不可用的时候它的Text显示为灰色,可是TextBlock不会htm

上例中UserName为TextBlock,Password为Label。对象

当Label禁用时候它的Content变为灰色的缘由是由于Label的默认模板中有一个触发器,当 Label禁用的时候它会设置Content的颜色。blog

若是要改变Label禁用时的样式能够在这改变。

Label比TextBlock更加复杂

以上说了Label至关于TextBlock的优点,下面说一下TextBlock的优点

加载Label时比TextBlock须要耗费更多的时间,不只仅是Label相对于直接继承于FrameElement的TextBlock有了更多层次的继承,它的visual tree更加复杂。

下面的图片告诉你是当你建立一个Label的时候后台都作了什么事情。

 

TextBlock的visual tree不包含任何子元素,而Label却复杂的多。它有一个border属性,最后经过一个TextBlock来显示内容。这样看来label其实就是一个个性化的TextBlock。

 

补充:

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是个不错的选择。
转载:http://www.cnblogs.com/junbird-nest/archive/2012/10/08/2715601.html
相关文章
相关标签/搜索