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>

설정

트랙백

댓글

아놔... 툴킷 만들었으면 제대로 관리하라고!!

.Net/Windows Phone 2011.12.22 11:03
헤이! 이것봐!!! 이런걸 보고 다 해주고 욕먹는거라자나!

툴킷 만들고 수정했으면 제대로 올려야지 이넘들아!

2011 Nov 버전이라고 해서 다운받아서 설치했더니...
설치된 버전은 2011 Oct 버전이자나!!!

아놔.. 한참 찾아해맸네...

msi 파일 다시 말아!

소스는 정상적이더군... 소스 내려받아서 수정완료! 

설정

트랙백

댓글