c# PasswordBoxHelper

 1  public class PasswordBoxHelper
 2     {
 3         public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordBoxHelper),
 4               new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
 5 
 6         public static string GetPassword(DependencyObject d)
 7         {
 8             return (string)d.GetValue(PasswordProperty);
 9         }
10         public static void SetPassword(DependencyObject d, string value)
11         {
12             d.SetValue(PasswordProperty, value);
13         }
14 
15         public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(PasswordBoxHelper),
16             new PropertyMetadata(new PropertyChangedCallback(OnAttachChanged)));
17 
18         public static string GetAttach(DependencyObject d)
19         {
20             return (string)d.GetValue(PasswordProperty);
21         }
22         public static void SetAttach(DependencyObject d, string value)
23         {
24             d.SetValue(PasswordProperty, value);
25         }
26 
27         static bool _isUpdating = false;
28         private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
29         {
30             PasswordBox pb = (d as PasswordBox);
31             pb.PasswordChanged -= Pb_PasswordChanged;
32             if (!_isUpdating)
33                 (d as PasswordBox).Password = e.NewValue.ToString();
34             pb.PasswordChanged += Pb_PasswordChanged;
35         }
36 
37         private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
38         {
39             PasswordBox pb = (d as PasswordBox);
40             pb.PasswordChanged += Pb_PasswordChanged;
41         }
42 
43         private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
44         {
45             PasswordBox pb = (sender as PasswordBox);
46             _isUpdating = true;
47             SetPassword(pb, pb.Password);
48             _isUpdating = false;
49         }
50     }

 

posted @ 2023-07-14 22:05  赵三毛  阅读(80)  评论(0)    收藏  举报