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

Getting Started

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

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

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}"/> 
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.

See Also

In this article
Not finding the help you need? Improve this article