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

xml


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

网络


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

运行上面的代码并单击 CheckBox1 以了解发生了什么。


当我单击 CheckBox 时,PasswordBox 如何显示12345 个字符?


因此,需要修复以下线路。


PasswordBox1.PasswordChar = CChar(" ")


慕标5832272
浏览 363回答 2
2回答

跃然一笑

这将适用于您正在寻找的内容,尽管它会在内存中公开您的密码。我们在 UI 的同一位置有一个文本框和一个密码框,当用户选中 Show Password 复选框时,我们折叠密码框并显示隐藏的文本框,同时更新文本。发送密码时,您需要检查是否使用了可见 ui 控件中的密码。Xml代码:<StackPanel Orientation="Horizontal">&nbsp; <Grid Width="300" Height="40">&nbsp; &nbsp; <PasswordBox Name="passwordBox" PasswordChar="*" />&nbsp; &nbsp; <TextBox Name="passwordTxtBox" Visibility="Collapsed" />&nbsp; </Grid><CheckBox Margin="10" Name="showPassword" Unchecked="ShowPassword_Unchecked"&nbsp; Checked="ShowPassword_Checked" /></StackPanel>后面的代码:&nbsp; &nbsp; private void ShowPassword_Checked(object sender, RoutedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; passwordTxtBox.Text = passwordBox.Password;&nbsp; &nbsp; &nbsp; &nbsp; passwordBox.Visibility = Visibility.Collapsed;&nbsp; &nbsp; &nbsp; &nbsp; passwordTxtBox.Visibility = Visibility.Visible;&nbsp; &nbsp; }&nbsp; &nbsp; private void ShowPassword_Unchecked(object sender, RoutedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; passwordBox.Password = passwordTxtBox.Text;&nbsp; &nbsp; &nbsp; &nbsp; passwordTxtBox.Visibility = Visibility.Collapsed;&nbsp; &nbsp; &nbsp; &nbsp; passwordBox.Visibility = Visibility.Visible;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP