diff MetroWpf/MetroWpf.Xaml/Controls/PasswordBoxHelper.cs @ 19:09d18d6e5f40

airport work
author stevenh7776 stevenhollidge@hotmail.com
date Thu, 15 Mar 2012 06:59:15 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Xaml/Controls/PasswordBoxHelper.cs	Thu Mar 15 06:59:15 2012 +0000
@@ -0,0 +1,100 @@
+
+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
+  }
+}