WPF - 標籤



Label 類為訪問鍵(也稱為助記符)提供功能和視覺支援。它常用於啟用對控制元件的快速鍵盤訪問。Label 類的繼承層次結構如下:

Hierarchical of Label

以下是 Label 類常用的屬性

序號 屬性及說明
1

Background

獲取或設定一個筆刷,該筆刷描述控制元件的背景。(繼承自 Control。)

2

Content

獲取或設定 ContentControl 的內容。(繼承自 ContentControl。)

3

ContentStringFormat

獲取或設定一個複合字符串,該字串指定如果 Content 屬性顯示為字串,則如何格式化它。(繼承自 ContentControl。)

4

ContentTemplate

獲取或設定用於顯示 ContentControl 內容的資料模板。(繼承自 ContentControl。)

5

ContextMenu

獲取或設定上下文選單元素,該元素應在每次透過使用者介面 (UI) 從此元素內部請求上下文選單時顯示。(繼承自 FrameworkElement。)

6

FontFamily

獲取或設定控制元件的字體系列。(繼承自 Control。)

7

FontSize

獲取或設定字型大小。(繼承自 Control。)

8

FontStyle

獲取或設定字型樣式。(繼承自 Control。)

9

FontWeight

獲取或設定指定字型的粗細或厚度。(繼承自 Control。)

10

Foreground

獲取或設定一個筆刷,該筆刷描述前景色。(繼承自 Control。)

11

Height

獲取或設定元素的建議高度。(繼承自 FrameworkElement。)

12

Margin

獲取或設定元素的外邊距。(繼承自 FrameworkElement。)

13

Name

獲取或設定元素的標識名稱。該名稱提供一個引用,以便程式碼隱藏(例如事件處理程式程式碼)在 XAML 處理器在處理過程中構造標記元素後引用它。(繼承自 FrameworkElement。)

14

Resources

獲取或設定本地定義的資源字典。(繼承自 FrameworkElement。)

15

Style

獲取或設定此元素在呈現時使用的樣式。(繼承自 FrameworkElement。)

16

Target

獲取或設定當用戶按下標籤的 . GTMT 時獲得焦點的元素。

17

Template

獲取或設定控制元件模板。(繼承自 Control。)

18

Width

獲取或設定元素的寬度。(繼承自 FrameworkElement。)

Label 類常用的事件

序號 事件及說明
1

ContextMenuOpening

當系統處理顯示上下文選單的互動時發生。

2

DragEnter

當輸入系統報告以該元素作為目標的基礎拖動事件時發生。(繼承自 UIElement)

3

DragLeave

當輸入系統報告以該元素作為源的基礎拖動事件時發生。(繼承自 UIElement)

4

DragOver

當輸入系統報告以該元素作為潛在放置目標的基礎拖動事件時發生。(繼承自 UIElement)

5

Drop

當輸入系統報告以該元素作為放置目標的基礎放置事件時發生。(繼承自 UIElement)

6

GotFocus

當 UIElement 獲取焦點時發生。(繼承自 UIElement)

7

KeyDown

當 UIElement 具有焦點時按下鍵盤鍵時發生。(繼承自 UIElement)

8

KeyUp

當 UIElement 具有焦點時釋放鍵盤鍵時發生。(繼承自 UIElement)

9

SizeChanged

當 FrameworkElement 上的 ActualHeight 或 ActualWidth 屬性的值發生變化時發生。(繼承自 FrameworkElement)

Label 類常用的方法

序號 方法及說明
1

Focus

聚焦 TextBlock,就像它是一個常規的可聚焦控制元件。

2

ToString

返回 Control 物件的字串表示形式。(繼承自 Control。)

示例

  • 讓我們建立一個名為WPFLabelControl的新 WPF 專案。

  • 從工具箱中拖動一個標籤控制元件。

  • 從屬性視窗更改標籤的不同屬性,如下面的 XAML 程式碼所示。

<Window x:Class = "WPFLabelControl.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:local = "clr-namespace:WPFLabelControl" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
	
   <Grid> 
      <Label x:Name = "label" Content = "Label Example in WPF" HorizontalAlignment = "Left"
         Margin = "71,82,0,0" VerticalAlignment = "Top" Height = "135" Width = "474" 
         Background = "#FFD6BEBE" FontFamily = "Snap ITC" FontSize = "36"
         FontStyle = "Italic" FontWeight = "Light" Foreground = "#FFBD6B6B"/> 
   </Grid> 
	
</Window>

編譯並執行上述程式碼後,將生成以下視窗。

Output of label
wpf_controls.htm
廣告

© . All rights reserved.