简体   繁体   English

Wpf PasswordBox当我单击CheckBox时必须显示字符

[英]Wpf PasswordBox must show characters when I click CheckBox

xaml XAML

<Window x:Class="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">
<StackPanel VerticalAlignment="Center" Width="300">
    <PasswordBox x:Name="PasswordBox1" Height="30" PasswordChar="*" Password="12345"/>
    <CheckBox x:Name="CheckBox1" Content="Show Password"/>
</StackPanel>
</Window>

vb.net vb.net

Class MainWindow 
Private Sub CheckBox1_Checked(sender As Object, e As RoutedEventArgs) Handles CheckBox1.Checked
    PasswordBox1.PasswordChar = CChar("")
End Sub
Private Sub CheckBox1_Unchecked(sender As Object, e As RoutedEventArgs) Handles CheckBox1.Unchecked
    PasswordBox1.PasswordChar = CChar("*")
End Sub
End Class

Run the above codes and click CheckBox1 in order to understand what is happening. 运行上面的代码,然后单击CheckBox1以了解发生了什么。

How can PasswordBox show characters which are 12345 when I click CheckBox? 单击CheckBox时,PasswordBox如何显示12345个字符?

So, following line need to be repaired. 因此,需要修复以下线路。

PasswordBox1.PasswordChar = CChar(" ") PasswordBox1.PasswordChar = CChar(“”)

This will work for what you are looking for although it will expose your passwords in memory. 尽管它将在内存中公开您的密码,但是这将满足您的查找需求。 We have a textbox and a password box in the same place on our UI and when the user checks the Show Password checkbox we collapse the passwordbox and show the hidden textbox, at the same time updating the text. 我们在用户界面的同一位置有一个文本框和一个密码框,当用户选中“显示密码”复选框时,我们会折叠密码框并显示隐藏的文本框,同时更新文本。 You will need to check you are using the password from the visible ui control when you send the password. 发送密码时,您需要检查您是否正在使用可见的ui控件中的密码。

Xaml code: XAML代码:

<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" />
</StackPanel>

Code behind: 后面的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM