New to Telerik UI for WPF? Download free 30-day trial

Getting Started with WPF PasswordBox

This tutorial will walk you through the creation of a sample application that contains RadPasswordBox.

Assembly References

In order to use the RadPasswordBox control in your projects, you have to add references to the following assembly:

  • Telerik.Windows.Controls

Adding Telerik Assemblies Using NuGet

To use RadPasswordBox when working with NuGet packages, install the Telerik.Windows.Controls.for.Wpf.Xaml package. The package name may vary slightly based on the Telerik dlls set - Xaml or NoXaml

Read more about NuGet installation in the Installing UI for WPF from NuGet Package article.

Adding RadPasswordBox to the Project

Before proceeding with adding RadPasswordBox to your project, make sure the required assembly references are added to the project.

You can add RadPasswordBox manually by writing the XAML code in Example 1. You can also add the control by dragging it from the Visual Studio Toolbox and dropping it over the XAML view.

Example 1: Adding RadPasswordBox in XAML

<telerik:RadPasswordBox Width="150" /> 

In order to use RadPasswordBox in XAML you have to add the namespace declaration shown in Example 2:

Example 2: Declaring Telerik Namespace

If you run the application you will see the PasswordBox as illustrated in Figure 1.

Figure 1: RadPasswordBox generated by the code in Example 1

WPF RadPasswordBox RadPasswordBox generated by the code in Example 1

Setting Watermark

When RadPasswordBox is empty and not focused, Watermark content can be shown. Example 2 demonstrates how to set Watermark text.

Example 2: Setting a watermark

<telerik:RadPasswordBox Width="150" WatermarkContent="enter a password" /> 
Figure 2 shows the result.

Figure 2: RadPasswordBoxBox with Watermark set

WPF RadPasswordBox RadPasswordBoxBox with Watermark set

Working with RadPasswordBox

The Text property of the RadPasswordBox contains only the sequence of masking characters set by the PasswordChar property. The actual input can be reached through the Password and SecurePassword properties. These properties are not dependency properties (cannot be bound) due to security reasons. To get these properties in MVVM, you can pass the RadPasswordBox element to a command from your view model. Let's demonstrate this with some code.

First we will declare the RadPasswordBox in XAML and bind a RadButton's Command property to a command from our view model.

Example 3: Declare the RadPasswordBox in XAML

<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" > 
    <telerik:RadPasswordBox x:Name="passwordBox" WatermarkContent="enter a password" Width="150" Margin="0 0 10 0" /> 
    <telerik:RadButton Content="Log in" Command="{Binding LoginCommand}" CommandParameter="{Binding ElementName=passwordBox}"/> 
</StackPanel> 
Now we just need to create our view model.

Example 3: Create ViewModel

public  class ViewModel 
{ 
    public System.Windows.Input.ICommand LoginCommand { get; set; } 
    public ViewModel() 
    { 
        LoginCommand = new DelegateCommand(OnLoginCommand_Executed); 
    } 
 
    private void OnLoginCommand_Executed(object obj) 
    { 
        var passwordBox = obj as RadPasswordBox; 
        if(passwordBox != null) 
        { 
            // actual entered password 
            var password = passwordBox.Password; 
        } 
    } 
} 
Voilà! Now when you click on the Login button, the LoginCommand.Execute() method will be called. Inside the method you can get the RadPasswordBox from the parameter and see the entered password from the Password property.

Setting a Theme

The controls from our suite support different themes. You can see how to apply a theme different than the default one in the Setting a Theme help article.

Changing the theme using implicit styles will affect all controls that have styles defined in the merged resource dictionaries. This is applicable only for the controls in the scope in which the resources are merged.

To change the theme, you can follow the steps below:

  • Choose between the themes and add reference to the corresponding theme assembly (ex: Telerik.Windows.Themes.Windows8.dll). You can see the different themes applied in the Theming examples from our WPF Controls Examples application.

  • Merge the ResourceDictionaries with the namespace required for the controls that you are using from the theme assembly. For the RadPasswordBox, you will need to merge the following resources:

    • Telerik.Windows.Controls
    • Telerik.Windows.Controls.Navigation

Example 4 demonstrates how to merge the ResourceDictionaries so that they are applied globally for the entire application.

Example 4: Merge the ResourceDictionaries

<Application.Resources> 
    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml"/> 
            <ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml"/> 
        </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

Alternatively, you can use the theme of the control via the StyleManager.

Figure 2 shows a RadPasswordBox with the Windows8 theme applied.

Figure 2: RadPasswordBox with the Windows8 theme

RadPasswordBox with Windows8 theme

Telerik UI for WPF Learning Resources

See Also

In this article