首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > windows >

windowsphone7上定制密码输入框

2012-09-14 
windowsphone7下定制密码输入框SDK中提供的PasswordBox很好用,但是不能实现显示密码的功能。个人通过组合Ph

windowsphone7下定制密码输入框

SDK中提供的PasswordBox很好用,但是不能实现显示密码的功能。

个人通过组合PhoneTextBox和PasswordBox来定制了一个用户控件,作为密码的输入框,并可以根据选择来实现隐藏或者显示密码。

xaml代码:

    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    }

仅作学习笔记,谢谢参考。




热点排行