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
}
仅作学习笔记,谢谢参考。
分享到:
相关推荐
本软件支持windows大部分软件密码输入框星号密码查看 忘记密码时可以通过本软件获取明文 亲测支持Foxmail7 带使用教程,欢迎下载!
windows phone 7 code
Windows Phone Blog Menu是一个Silverlight导航控件,看起来像Windows Phone 7。 控件中的tiles链接到web站点在你的博客中使用这个控件可以表达你对WP7的喜爱。 Windows Phone Blog Menu is a Silverlight ...
本书旨在分享强大的Microsoft编程工具(诸如Silverlight、XNA和Visual Studio 2010 Express Edition一类)的使用心得,介绍了如何使用现有的C#和.NET编程技能来开发一些Windows Phone 7应用程序。本书作者团队探讨了...
Windows Phone 7介绍,移动互联网大会资料
本书适合于Windows Phone 7开发入门的读者,也适合于从其他智能手机平台转向Windows Phone 7平台的读者,同时对于有Windows Phone 7开发经验的读者也极具参考价值。 本书系统论述了Windows Phone 7操作系统的应用...
Silverlight for Windows Phone 7
Programming windows phone 7 series
windows phone 7 自定义 输入法
This book is a gift from the Windows Phone 7 team at Microsoft to the programming community, and I am proud to have been a part of it. Within the pages that follow, I show you the basics of writing ...
windows phone 7 最新铃声,喜欢的可以下载用用
第四章 Silverlight for Windows Phone 7: Hello Windows Phone 7 第五章 当 Windows Azure 碰到了 Windows Phone 7 推送通知服务概述 Silverlight 交互开发: 第六章 了解 Windows Phone 7 网页浏览器控件 – Part...
《windows phone 7 完美开发征程》光盘上的代码
Microsoft Windows Phone 7 Connector for Mac 0.6 Beta
微软Windows Phone 7手机系统的使用手册,其英文说明简洁明了,配有大量操作图片,效果直观清晰,简单易懂,对初步认识WP7智能系统有很大的帮助!
Windows Phone7是微软重新打造windows mobile品牌之后推出的一款产品。
Windows Phone 7 手机备份程序 HD7 WPHONE HTC
ChevronWP7 - 著名的Windows Phone 7 解锁工具。 需要与ChevronWP7证书配套使用。 证书下载地址:http://download.csdn.net/source/3166513 防锁补丁:http://download.csdn.net/source/3166521
Windows Phone 7开发环境安装教程汇总,供学习参考。
目前该书一共24章,,对Windows Phone 7平台的特征、XAML在程序界面和布局方面,以及XNA游戏开发都做了深入的解读。