PhonePasswordBox

.Net/Windows Phone 2012.06.08 09:51

툴킷에 있는 PhoneTextBox 는 정말 맘에 드는 컨트롤 중 하나이다.

따로 TextBlock 을 둬서 TextBox에 어떤내용인지 설명을 하지 않아도 된다.

화면사이즈에 제약이 있는 Phone 환경에서는 꼭 필요한 컨트롤 중 하나이다.

 

하지만 PasswordBox 에서는 해당 기능이 지원되지 않는다.

그래서 찾아본 링크

http://damianblog.com/2011/01/21/wp7-password-watermark/

 

링크를 참조하여 내게 필요한 몇가지 기능을 추가했다.

아래는 소스.

======================== PhonePasswordBox.cs =======================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BloodAria.MyMusic.Controls
{
    public partial class PhonePasswordBox : UserControl
    {
        #region DependencyProperty

        public static readonly DependencyProperty HintProperty =
            DependencyProperty.Register("Hint", typeof(string), typeof(PhonePasswordBox), new PropertyMetadata("Password",
                    new PropertyChangedCallback(OnHintPropertyChanged)
                )
            );
        private static void OnHintPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            PhonePasswordBox phonePasswordBox = sender as PhonePasswordBox;
            if (phonePasswordBox != null && phonePasswordBox.txtBoxPasswordHint != null && args.NewValue != null)
            {
                phonePasswordBox.txtBoxPasswordHint.Text = args.NewValue as string;
            }
        }

        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(PhonePasswordBox), new PropertyMetadata(
                    new PropertyChangedCallback(OnPasswordPropertyChanged)
                )
            );
        private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            PhonePasswordBox phonePasswordBox = sender as PhonePasswordBox;
            if (phonePasswordBox != null && phonePasswordBox.txtBoxPassword != null && args.NewValue != null)
            {
                phonePasswordBox.txtBoxPassword.Password = args.NewValue as string;
                phonePasswordBox.CheckPasswordWatermark();
            }
        }

        #endregion

        #region Properties

        public string Hint
        {
            get { return base.GetValue(HintProperty) as string; }
            set { base.SetValue(HintProperty, value); }
        }
        public string Password
        {
            get { return base.GetValue(PasswordProperty) as string; }
            set { base.SetValue(PasswordProperty, value); }
        }

        #endregion

        #region Events

        public event RoutedEventHandler PasswordChanged;

        #endregion

        public PhonePasswordBox()
        {
            InitializeComponent();
            txtBoxPassword.PasswordChanged += (s, e) => this.Password = txtBoxPassword.Password;
            txtBoxPassword.PasswordChanged += (s, e) => { if (PasswordChanged != null) PasswordChanged(this, e); };
            txtBoxPasswordHint.TextChanged += (s, e) => this.Hint = txtBoxPasswordHint.Text;
            txtBoxPasswordHint.Text = "Password";
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            this.txtBoxPassword.Focus();
        }
       
        private void PasswordLostFocus(object sender, RoutedEventArgs e)
        {
            CheckPasswordWatermark();
        }
       
        public void CheckPasswordWatermark()
        {
            var passwordEmpty = string.IsNullOrEmpty(txtBoxPassword.Password);
            txtBoxPasswordHint.Opacity = passwordEmpty ? 100 : 0;
            txtBoxPassword.Opacity = passwordEmpty ? 0 : 100;
        }

        private void PasswordGotFocus(object sender, RoutedEventArgs e)
        {
            txtBoxPasswordHint.Opacity = 0;
            txtBoxPassword.Opacity = 100;
        }
    }
}
======================== PhonePasswordBox.xaml =======================

<UserControl x:Class="BloodAria.MyMusic.Controls.PhonePasswordBox"
    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"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="100" d:DesignWidth="400">

    <Grid>
        <TextBox x:Name="txtBoxPasswordHint" TextWrapping="Wrap" Foreground="{StaticResource PhoneTextBoxReadOnlyBrush}" IsHitTestVisible="False"/>
        <PasswordBox x:Name="txtBoxPassword" LostFocus="PasswordLostFocus" Opacity="0" GotFocus="PasswordGotFocus"/>
    </Grid>
</UserControl>

설정

트랙백

댓글