我发现passwordreveal方法不适用于wpf中的密码框,有什么方法可以通过复选框显示和隐藏密码吗?
我希望显示/隐藏 wpf 密码框中存在的功能
或者我只需要使用文本框来实现这个功能
PasswordBox
没有内置该功能。原始设计早于该选项在网站等上流行的时间。
一个简单的解决方案是构建一个
UserControl
,在普通 TextBox
或 PasswordBox
之间切换。
增强密码框.xaml
<UserControl x:Class="SOExamples.EnhancedPasswordBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<PasswordBox x:Name="_hidden"
PasswordChanged="_hidden_PasswordChanged" />
<TextBox x:Name="_visible"
Visibility="Collapsed"
TextChanged="_visible_TextChanged" />
<CheckBox x:Name="_isVisible"
Grid.Row="1"
Click="_isVisible_Click"
Content="Show"
HorizontalAlignment="Right" />
</Grid>
</UserControl>
增强密码框.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SOExamples
{
/// <summary>
/// Interaction logic for EnhancedPasswordBox.xaml
/// </summary>
public partial class EnhancedPasswordBox : UserControl
{
public EnhancedPasswordBox()
{
InitializeComponent();
}
#region string Password dependency property
public static DependencyProperty PasswordProperty = DependencyProperty.Register(
"Password",
typeof(string),
typeof(EnhancedPasswordBox),
new FrameworkPropertyMetadata(
(string)"DefaultValue",
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(obj, args) =>
{
((EnhancedPasswordBox)obj).OnPasswordChanged(args);
}));
public string Password
{
get
{
return (string)GetValue(PasswordProperty);
}
set
{
SetValue(PasswordProperty, value);
}
}
private void OnPasswordChanged(DependencyPropertyChangedEventArgs args)
{
if (_suspendChangeHandlers)
return;
_suspendChangeHandlers = true;
this._visible.Text = args.NewValue as string;
this._hidden.Password = args.NewValue as string;
_suspendChangeHandlers = false;
}
#endregion
private void _isVisible_Click(object sender, RoutedEventArgs e)
{
if (_isVisible.IsChecked == true)
{
this._hidden.Visibility = Visibility.Collapsed;
this._visible.Visibility = Visibility.Visible;
}
else
{
this._hidden.Visibility = Visibility.Visible;
this._visible.Visibility = Visibility.Collapsed;
}
}
private void _hidden_PasswordChanged(object sender, RoutedEventArgs e)
{
if (_suspendChangeHandlers)
return;
_suspendChangeHandlers = true;
this.SetCurrentValue(PasswordProperty, _hidden.Password);
this._visible.Text = _hidden.Password;
_suspendChangeHandlers = false;
}
private void _visible_TextChanged(object sender, TextChangedEventArgs e)
{
if (_suspendChangeHandlers)
return;
_suspendChangeHandlers = true;
this.SetCurrentValue(PasswordProperty, _visible.Text);
this._hidden.Password = _visible.Text;
_suspendChangeHandlers = false;
}
private bool _suspendChangeHandlers = false;
}
}
令人烦恼的是,PasswordBox 不允许您绑定到密码,但是
UserControl
公开了自己的 Password
属性来抽象该详细信息。