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