- WPF Tutorial
- WPF - Home
- WPF - Overview
- WPF - Environment Setup
- WPF - Hello World
- WPF - XAML Overview
- WPF - Elements Tree
- WPF - Dependency Properties
- WPF - Routed Events
- WPF - Controls
- WPF - Layouts
- WPF - Nesting Of Layout
- WPF - Input
- WPF - Command Line
- WPF - Data Binding
- WPF - Resources
- WPF - Templates
- WPF - Styles
- WPF - Triggers
- WPF - Debugging
- WPF - Custom Controls
- WPF - Exception Handling
- WPF - Localization
- WPF - Interaction
- WPF - 2D Graphics
- WPF - 3D Graphics
- WPF - Multimedia
- WPF Useful Resources
- WPF - Quick Guide
- WPF - Useful Resources
- WPF - Discussion
WPF - Passwordbox
PasswordBox is a control that allows the user to enter masked passwords. When the user enters a password, it will be displayed as password characters. You can change the Password character by setting the PasswordChar property. The hierarchical inheritance of PasswordBox class is as follows −
Commonly Used Properties of PasswordBox Class
Sr.No. | Property & Description |
---|---|
1 | InputScope Gets or sets the context for input used by this PasswordBox. |
2 | InputScopeProperty Identifies the InputScope dependency property. |
3 | IsPasswordRevealButtonEnabled Gets or sets a value that specifies whether the visual UI of the PasswordBox includes a button element that toggles showing or hiding the typed characters. In Windows 10 and later, use PasswordRevealMode instead. |
4 | IsPasswordRevealButtonEnabledProperty Identifies the IsPasswordRevealButtonEnabled dependency property. |
5 | MaxLength Gets or sets the maximum length for passwords to be handled by this PasswordBox. |
6 | MaxLengthProperty Identifies the MaxLength dependency property. |
7 | Password Gets or sets the password currently held by the PasswordBox. |
8 | PasswordChar Gets or sets the masking character for the PasswordBox. |
9 | PasswordCharProperty Identifies the PasswordChar dependency property. |
10 | PasswordProperty Identifies the Password dependency property. |
11 | PasswordRevealMode Gets or sets a value that specifies whether the password is always, never, or optionally obscured. |
12 | PasswordRevealModeProperty Identifies the PasswordRevealMode dependency property. |
13 | Resources Gets the locally defined resource dictionary. In XAML, you can establish resource items as child object elements of a frameworkElement.Resources property element, through XAML implicit collection syntax. (Inherited from FrameworkElement) |
Commonly Used Events of PasswordBox Class
Sr.No. | Event & Description |
---|---|
1 | ContextMenuOpening Occurs when the system processes an interaction that displays a context menu. |
2 | GotFocus Occurs when a UIElement receives focus. (Inherited from UIElement) |
3 | PasswordChanged Occurs when the value of the Password property changes. |
4 | Paste Occurs when text is pasted into the control. |
Below are the commonly used Methods of PasswordBox class.
Sr.No. | Method & Description |
---|---|
1 | OnLostFocus Called before the LostFocus event occurs. (Inherited from Control) |
2 | SelectAll Selects all the characters in the PasswordBox. |
3 | SetBinding Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement) |
4 | SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject) |
The following example shows the PasswordBox, labels, and button. Here is the XAML code in which all these controls are created and initialized.
<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>
Here is the button click event implementation in C# in which the program compares if the entered password is “wpf12345” then it will display the correct password message on the textblock.
using System.Windows; namespace WPFPasswordBoxControl { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { if (pwBox.Password.ToString() == "wpf12345") statusText.Text = "Password Accepted"; else statusText.Text = "Wrong Password"; } } }
When the above code is compiled and executed, it will produce the following window −
We recommend that you execute the above example code and try the other properties and events of PasswordBox class.