我正在尝试使用WPF密码,但我无法理解为什么它不起作用。
我希望隐藏密码,当用户点击眼睛时,密码就会显示出来。
我们的想法是创建一个隐藏的文本框和可见的密码框,当用户点击眼睛按钮时,文本框将可见,密码框将被隐藏,这是我的代码:
XAML:
<PasswordBox x:Name="passwordBox" HorizontalAlignment="Left" Height="27" Margin="102,37,0,0" VerticalAlignment="Top" Width="140"/>
<TextBox x:Name="password_textbox" HorizontalAlignment="Left" Height="27" Margin="102,37,0,0" VerticalAlignment="Top" Width="140" Visibility="Collapsed"/>
<Button x:Name="eye_button" Background="White" HorizontalAlignment="Left" Height="27" Margin="242,37,0,0" VerticalAlignment="Top" Width="30" MouseDown="MouseDown" MouseUp="MouseUp"/>
代码背后:
public MyPage()
{
InitializeComponent();
eye_button.PreviewMouseUp += MouseUp;
eye_button.PreviewMouseUp += MouseDown;
}
.
.
.
void MouseUp(Object sender , RoutedEventArgs args)
{
password_textbox.Text = "aaaaaaaa";
passwordBox.Visibility = Visibility.Collapsed;
password_textBox.Visibility = Visibility.Visible;
password_textBox.Focus();
}
void MouseDown(Object sender , RoutedEventArgs args)
{
passwordBox.Visibility = Visibility.Visible;
password_textBox.Visibility = Visibility.Collapsed;
passwordBox.Focus();
}