PasswordBox
As the user types, each character in a PasswordBox
replaced with a symbol—by default, a disc. We use the PasswordChanged
event handler to detect changes.
We use the Password property to read the value from the PasswordBox
. To begin, please drag a PasswordBox
control to your WPF window.
Here we specify the PasswordChanged
event handler. In PasswordBox_PasswordChanged
, we detect all modifications made to the input.
PasswordBox
. This returns a string
, one that contains the original characters typed.<Window x:Class="WpfApplication23.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <PasswordBox HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="100" PasswordChanged="PasswordBox_PasswordChanged"/> </Grid> </Window>using System.Windows; using System.Windows.Controls; namespace WpfApplication23 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) { // ... Display Password in Title. // A bad design decision. var box = sender as PasswordBox; this.Title = "Password typed: " + box.Password; } } }
PasswordChar
Another property available is PasswordChar
. When unspecified, this is left as the default value, a filled-in circle (bullet). You can use any character.
PasswordChar
as the default. The disc is a standard on web sites and program interfaces.How can you access the Password from a PasswordBox
? We can assign a member field in the Window class
to the value of the Password property in PasswordChanged
.
PasswordBox
when the Button
is clicked.PasswordBox
when the button is clicked.But in places where passwords are required, PasswordBox
helps avoid program development steps. We can access the Password property and use the PasswordChanged
event handler.