A good way to show password in PasswordBox

怎甘沉沦 提交于 2019-11-29 15:23:55

If you want your password to be visible you should use a textbox. The only function of a password box is to mask input. It doesn't provide extra functionality. Its also possible to switch your textbox and password box at runtime

There are no any inbuilt properties to show password character in PasswordBox control. But we could do this by TextBox control to display Password in PasswordBox. For PasswordBox with show/hide functionality in WPF. We will use Two TextBlock controls, TextBox control, PasswordBox control, Image control

XAML

<Grid>
    <TextBlock Text="Welcome&#xa;To see the Password"
               FontSize="28"
               FontWeight="Bold"
               HorizontalAlignment="Center"
               VerticalAlignment="Top"
               TextAlignment="Center"
               Margin="0,30,0,0" />
    <TextBlock Text="Enter your password"
               FontSize="20"
               HorizontalAlignment="Left"
               Margin="112,160,0,0"
               VerticalAlignment="Top" />
    <TextBox Height="40"
             FontSize="20"
             Padding="5,2,47,0"
             HorizontalAlignment="Left"
             Margin="112,193,0,0"
             Name="txtVisiblePasswordbox"
             VerticalAlignment="Top"
             Width="274" />
    <PasswordBox Height="40"
                 FontSize="20"
                 Padding="5,2,47,0" 
                 HorizontalAlignment="Left"
                 Margin="112,193,0,0"
                 Name="txtPasswordbox"
                 VerticalAlignment="Top"
                 Width="274"
                 PasswordChanged="txtPasswordbox_PasswordChanged" />
    <Image Visibility="Hidden"
           Height="30"
           HorizontalAlignment="Left"
           Name="ImgShowHide"
           Stretch="Fill"
           VerticalAlignment="Top"
           Width="30"
           Margin="351,198,0,0"
           MouseLeave="ImgShowHide_MouseLeave"
           PreviewMouseDown="ImgShowHide_PreviewMouseDown"
           PreviewMouseUp="ImgShowHide_PreviewMouseUp" />
</Grid>

Code Behind

string AppPath = Directory.GetCurrentDirectory();
public MainWindow()
{
    InitializeComponent();
    ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\\img\\clip.jpg"));
}
private void ImgShowHide_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
    HidePassword();
}

private void ImgShowHide_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    ShowPassword();
}
private void ImgShowHide_MouseLeave(object sender, MouseEventArgs e)
{
    HidePassword();
}
private void txtPasswordbox_PasswordChanged(object sender, RoutedEventArgs e)
{
    if(txtPasswordbox.Password.Length > 0)
        ImgShowHide.Visibility = Visibility.Visible;
    else
        ImgShowHide.Visibility = Visibility.Hidden;
}

void ShowPassword()
{
    ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\\img\\cus.jpg"));
    txtVisiblePasswordbox.Visibility = Visibility.Visible;
    txtPasswordbox.Visibility = Visibility.Hidden;
    txtVisiblePasswordbox.Text = txtPasswordbox.Password;
}
void HidePassword()
{
    ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\\img\\clip.jpg"));
    txtVisiblePasswordbox.Visibility = Visibility.Hidden;
    txtPasswordbox.Visibility = Visibility.Visible;
    txtPasswordbox.Focus();
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!