- XAML 教程
- XAML - 首頁
- XAML - 概述
- XAML - 環境設定
- 在 macOS 上編寫 XAML 應用程式
- XAML 與 C# 程式碼
- XAML 與 VB.NET
- XAML - 構建塊
- XAML - 控制元件
- XAML - 佈局
- XAML - 事件處理
- XAML - 資料繫結
- XAML - 標記擴充套件
- XAML - 依賴屬性
- XAML - 資源
- XAML - 模板
- XAML - 樣式
- XAML - 觸發器
- XAML - 除錯
- XAML - 自定義控制元件
- XAML 有用資源
- XAML - 快速指南
- XAML - 有用資源
- XAML - 討論
XAML - 密碼框
PasswordBox 是一種控制元件,使用者可以在其中輸入掩碼密碼。當用戶輸入密碼時,文字不會顯示,只會顯示密碼字元。密碼字元(通常顯示為 *)可以透過 **PasswordChar** 屬性輕鬆更改。PasswordBox 類的層次繼承如下:
屬性
| 序號 | 屬性及描述 |
|---|---|
| 1 | InputScope 獲取或設定此 PasswordBox 使用的輸入上下文。 |
| 2 | InputScopeProperty 標識 InputScope 依賴屬性。 |
| 3 | IsPasswordRevealButtonEnabled 獲取或設定一個值,該值指定 PasswordBox 的可視 UI 是否包含一個按鈕元素,該元素可切換顯示或隱藏輸入的字元。在 Windows 10 及更高版本中,請改用 PasswordRevealMode。 |
| 4 | IsPasswordRevealButtonEnabledProperty 標識 IsPasswordRevealButtonEnabled 依賴屬性。 |
| 5 | MaxLength 獲取或設定此 PasswordBox 處理的密碼的最大長度。 |
| 6 | MaxLengthProperty 標識 MaxLength 依賴屬性。 |
| 7 | Password 獲取或設定 PasswordBox 當前持有的密碼。 |
| 8 | PasswordChar 獲取或設定 PasswordBox 的掩碼字元。 |
| 9 | PasswordCharProperty 標識 PasswordChar 依賴屬性。 |
| 10 | PasswordProperty 標識 Password 依賴屬性。 |
| 11 | PasswordRevealMode 獲取或設定一個值,該值指定密碼始終、從不或可選地是否被遮蔽。 |
| 12 | PasswordRevealModeProperty 標識 PasswordRevealMode 依賴屬性。 |
| 13 | Resources 獲取本地定義的資源字典。在 XAML 中,您可以將資源項作為 FrameworkElement.Resources 屬性元素的子物件元素建立,透過 XAML 隱式集合語法。(繼承自 FrameworkElement) |
事件
| 序號 | 事件及描述 |
|---|---|
| 1 | ContextMenuOpening 當系統處理顯示上下文選單的互動時發生。 |
| 2 | GotFocus 當 UIElement 獲取焦點時發生。(繼承自 UIElement) |
| 3 | PasswordChanged 當 Password 屬性的值更改時發生。 |
| 4 | Paste 當文字貼上到控制元件中時發生。 |
方法
| 序號 | 方法及描述 |
|---|---|
| 1 | OnLostFocus 在 LostFocus 事件發生之前呼叫。(繼承自 Control) |
| 2 | SelectAll 選擇 PasswordBox 中的所有字元。 |
| 3 | SetBinding 使用提供的繫結物件將繫結附加到 FrameworkElement。(繼承自 FrameworkElement) |
| 4 | SetValue 設定 DependencyObject 上依賴屬性的本地值。(繼承自 DependencyObject) |
示例
以下示例顯示了 PasswordBox、標籤和按鈕。以下是建立和初始化所有這些控制元件的 XAML 程式碼。
<Window x:Class = "PasswordBox.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>
<PasswordBox x:Name = "pwBox"
Height = "35"
Width = "200"
MaxLength = "8"
Margin = "159,55,158,229" />
<Label Content = "Password"
HorizontalAlignment = "Left"
Margin = "108,61,0,0"
VerticalAlignment = "Top"
Width = "70" />
<Button Content = "Ok" HorizontalAlignment = "Left"
Margin = "406,64,0,0"
VerticalAlignment = "Top"
Width = "75" Click = "Button_Click"/>
<Label Name = "statusText"
HorizontalAlignment = "Left"
Margin = "159,128,0,0"
VerticalAlignment = "Top"
Width = "200"
Height = "38"/>
</Grid>
</Window>
以下是 C# 中的按鈕單擊事件實現,程式在其中比較密碼。如果輸入的密碼為“xaml1234”,則它將在標籤上顯示訊息“密碼正確”。
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace XAMLMenu {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e) {
MenuItem item = sender as MenuItem;
this.Title = "File: " + item.Header;
}
private void MenuItem_Click1(object sender, RoutedEventArgs e) {
MenuItem item = sender as MenuItem;
this.Title = "Edit: " + item.Header;
}
}
}
編譯並執行上述程式碼後,將生成以下輸出:
建議您執行上述示例程式碼,並嘗試一些其他屬性和事件。