XAML - PasswordBox



A PasswordBox is a control in which the user can enter a masked password. When the user enters a password, the text is not displayed, only the password characters are shown. The password character (usually shown as *) can be easily changed by the PasswordChar property. The hierarchical inheritance of PasswordBox class is as follows −

PasswordBox Hierarchy

Properties

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)

Events

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.

Methods

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)

Example

The following example shows the PasswordBox, labels, and a button. Here is the XAML code to create and initialize all these controls.

<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 the password. If the entered password is “xaml1234”, then it will display the message "correct password" on the label.

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; 
      } 
   } 
}

When you compile and execute the above code, it will produce the following output −

PasswordBox Output

We recommend you to execute the above example code and experiment with some other properties and events.

xaml_controls.htm
Advertisements