SDK中提供的PasswordBox很好用,但是不能实现显示密码的功能。
个人通过组合PhoneTextBox和PasswordBox来定制了一个用户控件,作为密码的输入框,并可以根据选择来实现隐藏或者显示密码。
xaml代码:
<UserControl x:Class="CustomControls.PasswordTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tk="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}">
<UserControl.Resources>
<Style x:Name="MyCustomTextBoxStyle" TargetType="tk:PhoneTextBox">
<Setter Property="Width" Value="375"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="-5"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Name="CustomPasswordBoxStyle" TargetType="PasswordBox">
<Setter Property="FontSize" Value="22"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="5 0 0 0"/>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<tk:PhoneTextBox x:Name="ShowPwdTB"
Hint="请输入数字或字母"
Style="{StaticResource MyCustomTextBoxStyle}"
Visibility="Visible" TextChanged="OnShowPwdChanged"
GotFocus="OnShowPwdTBGotFocus"/>
<PasswordBox x:Name="HidePwdTB" Style="{StaticResource CustomPasswordBoxStyle}"
LostFocus="OnHidePwdTBLostFocus"
Padding="8 0 0 0" Visibility="Collapsed"/>
</Grid>
</UserControl>
.cs文件代码:
public partial class PasswordTextBox : UserControl
{
#region Dependency Properties
public static readonly DependencyProperty IsShowPasswordProperty =
DependencyProperty.Register("IsShowPassword", typeof(bool), typeof(PasswordTextBox),
new PropertyMetadata(OnIsShowPasswordPropertyChanged));
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register("Password", typeof(string), typeof(PasswordTextBox),
new PropertyMetadata(OnPasswordPropertyChanged));
#endregion
#region Data Members
// 当密码为空时,保留ShowPwdTB的可见性(用于显示Hint文本);此时设置隐藏密码的操作无效
private bool m_IsNeedHidePassword;
#endregion
#region Constructor
public PasswordTextBox()
{
InitializeComponent();
m_IsNeedHidePassword = false;
}
#endregion
#region Public Methods
public bool IsShowPassword
{
get
{
return (bool)GetValue(IsShowPasswordProperty);
}
set
{
SetValue(IsShowPasswordProperty, value);
}
}
public string Password
{
get
{
if (ShowPwdTB.Visibility == Visibility.Visible)
{
return ShowPwdTB.Text;
}
else
{
return HidePwdTB.Password;
}
}
set
{
SetValue(PasswordProperty, value);
}
}
#endregion
#region Event Handler
private void OnShowPwdChanged(object sender, EventArgs e)
{
if (ShowPwdTB.Visibility == Visibility.Visible && ShowPwdTB.Text.Length == 0)
{
ShowPwdTB.Hint = AppRes.PasswordHintText;
}
else
{
ShowPwdTB.Hint = string.Empty;
}
}
private void OnShowPwdTBGotFocus(object sender, EventArgs e)
{
if ((!IsShowPassword && ShowPwdTB.Text.Length == 0) || m_IsNeedHidePassword)
{
m_IsNeedHidePassword = false;
HidePasswordTB();
HidePwdTB.Focus();
}
}
private void OnHidePwdTBLostFocus(object sender, EventArgs e)
{
if (HidePwdTB.Password.Length == 0)
{
ShowPasswordTB();
ShowPwdTB.Text = string.Empty;
ShowPwdTB.Hint = AppRes.PasswordHintText;
}
}
private static void OnIsShowPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PasswordTextBox pwdTB = d as PasswordTextBox;
if ((bool)e.NewValue)
{
pwdTB.ShowPwdTB.Visibility = Visibility.Visible;
pwdTB.HidePwdTB.Visibility = Visibility.Collapsed;
pwdTB.ShowPwdTB.Text = pwdTB.HidePwdTB.Password;
pwdTB.HidePwdTB.Password = string.Empty;
pwdTB.m_IsNeedHidePassword = false;
}
else
{
if (pwdTB.ShowPwdTB.Text.Length == 0)
{
pwdTB.m_IsNeedHidePassword = true;
return;
}
pwdTB.ShowPwdTB.Visibility = Visibility.Collapsed;
pwdTB.HidePwdTB.Visibility = Visibility.Visible;
pwdTB.HidePwdTB.Password = pwdTB.ShowPwdTB.Text;
pwdTB.ShowPwdTB.Text = string.Empty;
}
}
private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PasswordTextBox pwdTB = d as PasswordTextBox;
if (pwdTB.ShowPwdTB.Visibility == Visibility.Visible)
{
pwdTB.ShowPwdTB.Text = (string)e.NewValue;
}
else
{
pwdTB.HidePwdTB.Password = (string)e.NewValue;
}
}
#endregion
#region Private Methods
private void ShowPasswordTB()
{
ShowPwdTB.Visibility = Visibility.Visible;
HidePwdTB.Visibility = Visibility.Collapsed;
}
private void HidePasswordTB()
{
ShowPwdTB.Visibility = Visibility.Collapsed;
HidePwdTB.Visibility = Visibility.Visible;
}
#endregion
}
仅作学习笔记,谢谢参考。