WPF PasswordBox - PasswordRevealMode was not found in type 'PasswordBox'

Stephen Wilkinson 11 Reputation points
2020-09-18T17:34:34.983+00:00

The title says it all really.

I've trying to make a log in window that uses a PasswordBox to mask the password; but I want the user to be able to peek at it if they suspect that they might have made a mistake.

I've read the Microsoft documentation and it says to use the PasswordRevealMode property. However, whenever I try it, either in the xaml or the code behind I get the following error:

PasswordRevealMode was not found in type 'PasswordBox'

Is this a bug with VS Community 2019? Or do I need to do something else to get it to work?

Developer technologies | Windows Presentation Foundation
{count} vote

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2020-09-21T02:47:23.877+00:00

    PasswordRevealMode belongs to Windows Runtime API which used in UWP not WPF, but you can use some methods to implement the function in WPF. Use a TextBox to show the PasswordBox.Password depending on needs is a very simple way.Look like below picture shown:
    26061-4.gif

    The xaml code is:

    <StackPanel Orientation="Horizontal">  
                <Grid Width="300" Height="40">  
                    <PasswordBox Name="passwordBox" PasswordChar="*" />  
                    <TextBox Name="passwordTxtBox" Visibility="Collapsed" />  
                </Grid>  
                <CheckBox Margin="10" Name="showPassword" Unchecked="ShowPassword_Unchecked"  Checked="ShowPassword_Checked" HorizontalAlignment="Center" VerticalAlignment="Center"/>  
            </StackPanel>  
      
    

    The cs code is:

    private void ShowPassword_Checked(object sender, RoutedEventArgs e)  
            {  
                passwordTxtBox.Text = passwordBox.Password;  
                passwordBox.Visibility = Visibility.Collapsed;  
                passwordTxtBox.Visibility = Visibility.Visible;  
            }  
      
            private void ShowPassword_Unchecked(object sender, RoutedEventArgs e)  
            {  
                passwordBox.Password = passwordTxtBox.Text;  
                passwordTxtBox.Visibility = Visibility.Collapsed;  
                passwordBox.Visibility = Visibility.Visible;  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

  2. Lloyd Sheen 1,486 Reputation points
    2020-09-18T18:04:38.57+00:00

    PasswordRevealMode is only used in UWP, not WPF.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.