XAML - 按鈕



Button 類表示最基本的按鈕控制元件型別。Button 類的層次繼承如下:

Button Hierarchy

屬性

序號 屬性及描述
1

Background

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

2

BorderBrush

獲取或設定一個筆刷,描述控制元件的邊框填充。(繼承自 Control)

3

BorderThickness

獲取或設定控制元件的邊框粗細。(繼承自 Control)

4

Content

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

5

ClickMode

獲取或設定一個值,指示 Click 事件何時發生,以裝置行為表示。(繼承自 ButtonBase)

6

ContentTemplate

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

7

FontFamily

獲取或設定用於在控制元件中顯示文字的字型。(繼承自 Control)

8

FontSize

獲取或設定此控制元件中文字的大小。(繼承自 Control)

9

FontStyle

獲取或設定呈現文字的樣式。(繼承自 Control)

10

FontWeight

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

11

Foreground

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

12

Height

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

13

HorizontalAlignment

獲取或設定當 FrameworkElement 在佈局父級(如面板或專案控制元件)中組合時應用的水平對齊特徵。(繼承自 FrameworkElement)

14

IsEnabled

獲取或設定一個值,指示使用者是否可以與控制元件互動。(繼承自 Control)

15

IsPressed

獲取一個值,指示 ButtonBase 當前是否處於按下狀態。(繼承自 ButtonBase)

16

Margin

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

17

Name

獲取或設定物件的標識名稱。當 XAML 處理器從 XAML 標記建立物件樹時,執行時程式碼可以透過此名稱引用 XAML 宣告的物件。(繼承自 FrameworkElement)

18

Opacity

獲取或設定物件的透明度。(繼承自 UIElement)

19

Resources

獲取本地定義的資源字典。在 XAML 中,您可以將資源項作為 frameworkElement 的子物件元素建立。Resources 屬性元素,透過 XAML 隱式集合語法。(繼承自 FrameworkElement)

20

Style

獲取或設定一個 Style 例項,在佈局和渲染期間應用於此物件。(繼承自 FrameworkElement)

21

Template

獲取或設定控制元件模板。控制元件模板定義 UI 中控制元件的視覺外觀,並在 XAML 標記中定義。(繼承自 Control)

22

VerticalAlignment

獲取或設定當 FrameworkElement 在父物件(如面板或專案控制元件)中組合時應用的垂直對齊特徵。(繼承自 FrameworkElement)

23

Visibility

獲取或設定 UIElement 的可見性。不可見的 UIElement 不會呈現,也不會將其所需的大小傳達給佈局。(繼承自 UIElement)

24

Width

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

方法

序號 方法及描述
1

ClearValue

清除依賴屬性的區域性值。(繼承自 DependencyObject)

2

FindName

檢索具有指定標識名稱的物件。(繼承自 FrameworkElement)

3

OnApplyTemplate

每當應用程式程式碼或內部程序(例如重建佈局傳遞)呼叫 ApplyTemplate 時都會呼叫。簡單來說,這意味著在 UI 元素在應用程式中顯示之前會呼叫該方法。覆蓋此方法以影響類的預設模板後邏輯。(繼承自 FrameworkElement)

4

OnContentChanged

當 Content 屬性的值發生更改時呼叫。(繼承自 ContentControl)

5

OnDragEnter

在 DragEnter 事件發生之前呼叫。(繼承自 Control)

6

OnDragLeave

在 DragLeave 事件發生之前呼叫。(繼承自 Control)

7

OnDragOver

在 DragOver 事件發生之前呼叫。(繼承自 Control)

8

OnDrop

在 Drop 事件發生之前呼叫。(繼承自 Control)

9

OnGotFocus

在 GotFocus 事件發生之前呼叫。(繼承自 Control)

10

OnKeyDown

在 KeyDown 事件發生之前呼叫。(繼承自 Control)

11

OnKeyUp

在 KeyUp 事件發生之前呼叫。(繼承自 Control)

12

OnLostFocus

在 LostFocus 事件發生之前呼叫。(繼承自 Control)

13

SetBinding

使用提供的繫結物件將繫結附加到 FrameworkElement。(繼承自 FrameworkElement)

事件

序號 事件及描述
1

Click

當單擊按鈕控制元件時發生。(繼承自 ButtonBase)

2

DataContextChanged

當 FrameworkElement.DataContext 屬性的值發生更改時發生。(繼承自 FrameworkElement)

3

DragEnter

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

4

DragLeave

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

5

DragOver

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

6

DragStarting

當啟動拖動操作時發生。(繼承自 UIElement)

7

GotFocus

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

8

Holding

當在此元素的命中測試區域上發生其他未處理的 Hold 互動時發生。(繼承自 UIElement)

9

IsEnabledChanged

當 IsEnabled 屬性發生更改時發生。(繼承自 Control)

10

OnKeyDown

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

11

OnKeyUp

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

12

LostFocus

當 UIElement 失去焦點時發生。(繼承自 UIElement)

13

SizeChanged

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

示例

以下示例包含三個按鈕,它們根據其 ClickMode 屬性值的不同而做出不同的響應。

以下是建立三個按鈕並設定一些屬性和單擊事件的 XAML 程式碼。

<Window x:Class = "XAMLButton.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "350" Width = "604"> 
		  
   <Grid> 
      <StackPanel Margin = "10"> 
         <Button x:Name = "button1"  
            Content = "Hover" 
            Click = "OnClick1" 
            ClickMode = "Hover" 
            Margin = "10"  
            Width = "150" 
            HorizontalAlignment = "Center" 
            Foreground = "Gray"/> 
			<Button x:Name = "button2"  
            Content = "Press to Click" 
            Click = "OnClick2"  
            ClickMode = "Press" 
            Margin = "10"  
            Width = "150"  
            HorizontalAlignment = "Center"  
            Foreground = "DarkBlue"/> 
			<Button x:Name = "button3"  
            Content = "Release" 
            Click = "OnClick3"  
            ClickMode = "Release" 
            Margin = "10"  
            Width = "150" 
            HorizontalAlignment = "Center"/> 
      </StackPanel> 
   </Grid>
   
</Window>

以下是 C# 中的單擊事件實現。

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media;  

namespace XAMLButton { 
   /// <summary> 
      /// Interaction logic for MainWindow.xaml 
   /// </summary>
	public partial class MainWindow : Window {
      public MainWindow() { 
         InitializeComponent(); 
      }  
      void OnClick1(object sender, RoutedEventArgs e) { 
         button1.Foreground = new SolidColorBrush(Colors.Blue); 
         MessageBox.Show("On Hover click event occurs."); 
      } 
      void OnClick2(object sender, RoutedEventArgs e) { 
         button2.Foreground = new SolidColorBrush(Colors.Green); 
         MessageBox.Show("On Press click event occurs."); 
      } 
      void OnClick3(object sender, RoutedEventArgs e) { 
         button1.Foreground = new SolidColorBrush(Colors.Green); 
         button2.Foreground = new SolidColorBrush(Colors.Blue); 
         MessageBox.Show("On Release click event occurs."); 
      }
   } 
} 

編譯並執行以上程式碼後,將生成以下螢幕:

Event Implementation Output

當滑鼠進入第一個按鈕區域時,將顯示以下訊息:

First Button Region

按下第二個按鈕時,將顯示以下訊息:

Second Button

單擊後釋放最後一個按鈕時,將顯示以下訊息:

Last Button

建議您執行以上示例程式碼,並嘗試一些其他屬性和事件。

xaml_controls.htm
廣告

© . All rights reserved.