comparison Chronosv2/source/Presentation/Windows/Helpers/PasswordBoxHelper.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System.Windows;
2 using System.Windows.Controls;
3
4 namespace Chronos.Presentation.Windows.Helpers
5 {
6 /// <summary>
7 /// This class adds binding capabilities to the standard WPF PasswordBox.
8 /// </summary>
9 /// <remarks>
10 /// http://www.codeproject.com/Articles/37167/Binding-Passwords.aspx
11 /// </remarks>
12 public static class PasswordBoxHelper
13 {
14 #region · Static Members ·
15
16 private static bool IsUpdating = false;
17
18 #endregion
19
20 #region · Attached Properties ·
21
22 /// <summary>
23 /// BoundPassword Attached Dependency Property
24 /// </summary>
25 public static readonly DependencyProperty BoundPasswordProperty =
26 DependencyProperty.RegisterAttached("BoundPassword",
27 typeof(string),
28 typeof(PasswordBoxHelper),
29 new FrameworkPropertyMetadata(string.Empty, OnBoundPasswordChanged));
30
31 #endregion
32
33 #region · Attached Property Get/Set Methods ·
34
35 /// <summary>
36 /// Gets the BoundPassword property.
37 /// </summary>
38 public static string GetBoundPassword(DependencyObject d)
39 {
40 return (string)d.GetValue(BoundPasswordProperty);
41 }
42
43 /// <summary>
44 /// Sets the BoundPassword property.
45 /// </summary>
46 public static void SetBoundPassword(DependencyObject d, string value)
47 {
48 d.SetValue(BoundPasswordProperty, value);
49 }
50
51 #endregion
52
53 #region · Attached Properties Callbacks ·
54
55 /// <summary>
56 /// Handles changes to the BoundPassword property.
57 /// </summary>
58 private static void OnBoundPasswordChanged(
59 DependencyObject d,
60 DependencyPropertyChangedEventArgs e)
61 {
62 PasswordBox password = d as PasswordBox;
63
64 if (password != null)
65 {
66 // Disconnect the handler while we're updating.
67 password.PasswordChanged -= PasswordChanged;
68 }
69
70 if (e.NewValue != null)
71 {
72 if (!IsUpdating)
73 {
74 password.Password = e.NewValue.ToString();
75 }
76 }
77 else
78 {
79 password.Password = string.Empty;
80 }
81
82 // Now, reconnect the handler.
83 password.PasswordChanged += new RoutedEventHandler(PasswordChanged);
84 }
85
86 /// <summary>
87 /// Handles the password change event.
88 /// </summary>
89 static void PasswordChanged(object sender, RoutedEventArgs e)
90 {
91 PasswordBox password = sender as PasswordBox;
92 IsUpdating = true;
93 SetBoundPassword(password, password.Password);
94 IsUpdating = false;
95 }
96
97 #endregion
98 }
99 }