WPF學習二:TextBlock和Label的區別


TextBlock和Label都是用來顯示少量數據的。好多文章對Label存在的描述都是它允許使用"快速獲取"。"快速獲取"就是允許你用Alt加上其它的按鍵快速和UI界面的某個控件交互,比如你可以用ALT加上O鍵來點擊一個OK按鈕。

TextBlock直接繼承於FrameworkElement,而Label繼承於ContentControl。這樣看來,Label可以做這樣的事情:

1.可以定義一個控件模板(通過Template屬性)

2.可以顯示出string以外的其他信息(通過Content屬性)

3.為Label內容添加一個DataItemplate(通過ContentTemplate屬性)

4.做一些FrameworkElement元素不能做的事情

下邊是一個TextBlock和Label的繼承關系圖

當Label不可用的時候它的Text顯示為灰色,但是TextBlock不會

上例中UserName為TextBlock,Password為Label。

當Label禁用時候它的Content變為灰色的原因是因為Label的默認模板中有一個觸發器,當 Label禁用的時候它會設置Content的顏色。

如果要改變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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM