view MetroWpf/MetroWpf.Xaml/Controls/PasswordBoxHelper.cs @ 33:f3a0641c1586

bat files added to Messaging solution
author adminsh@apollo
date Thu, 22 Mar 2012 08:11:42 +0000
parents 09d18d6e5f40
children
line wrap: on
line source


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

namespace MetroWpf.Xaml.Controls
{
  /// <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
  }
}