view Chronosv2/source/Presentation/Windows/Helpers/PasswordBoxHelper.cs @ 12:6a0449185449

SCC changed from TFS to HG
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:47:35 +0700
parents 443821e55f06
children
line wrap: on
line source

using System.Windows;
using System.Windows.Controls;

namespace Chronos.Presentation.Windows.Helpers
{
    /// <summary>
    /// This class adds binding capabilities to the standard WPF PasswordBox.
    /// </summary>
    /// <remarks>
    /// http://www.codeproject.com/Articles/37167/Binding-Passwords.aspx
    /// </remarks>
    public static class PasswordBoxHelper
    {
        #region · Static Members ·

        private static bool IsUpdating = false;

        #endregion

        #region · Attached Properties ·

        /// <summary>
        /// BoundPassword Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty BoundPasswordProperty =
            DependencyProperty.RegisterAttached("BoundPassword",
                typeof(string),
                typeof(PasswordBoxHelper),
                new FrameworkPropertyMetadata(string.Empty, OnBoundPasswordChanged));

        #endregion

        #region · Attached Property Get/Set Methods ·

        /// <summary>
        /// Gets the BoundPassword property.
        /// </summary>
        public static string GetBoundPassword(DependencyObject d)
        {
            return (string)d.GetValue(BoundPasswordProperty);
        }

        /// <summary>
        /// Sets the BoundPassword property.
        /// </summary>
        public static void SetBoundPassword(DependencyObject d, string value)
        {
            d.SetValue(BoundPasswordProperty, value);
        }

        #endregion

        #region · Attached Properties Callbacks ·

        /// <summary>
        /// Handles changes to the BoundPassword property.
        /// </summary>
        private static void OnBoundPasswordChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            PasswordBox password = d as PasswordBox;

            if (password != null)
            {
                // Disconnect the handler while we're updating.
                password.PasswordChanged -= PasswordChanged;
            }

            if (e.NewValue != null)
            {
                if (!IsUpdating)
                {
                    password.Password = e.NewValue.ToString();
                }
            }
            else
            {
                password.Password = string.Empty;
            }

            // Now, reconnect the handler.
            password.PasswordChanged += new RoutedEventHandler(PasswordChanged);
        }

        /// <summary>
        /// Handles the password change event.
        /// </summary>
        static void PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox password = sender as PasswordBox;
            IsUpdating = true;
            SetBoundPassword(password, password.Password);
            IsUpdating = false;
        }

        #endregion
    }
}