annotate MetroWpf/MetroWpf.Xaml/Controls/PasswordBoxHelper.cs @ 121:8f94475d3146 tip

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