wpf prism Password绑定问题

1.新建一个类PasswordBoxHelper,用来给Password附加依赖属性

 

2.在PasswordBoxHelper类中:

 1 public class PasswordBoxHelper
 2     {
 3         /// <summary>
 4         /// 定义表示属性的对象,它是DependencyProperty类的实例
 5         /// </summary>
 6         public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("PasswordContent", typeof(string), typeof(PasswordBoxHelper),
 7             new FrameworkPropertyMetadata(string.Empty,new PropertyChangedCallback( OnPropertyChangedCallback)));
 8         public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordBoxHelper),
 9             new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnAttachPropertyChangedCallback)));
10 
11         
12 
13         public static bool GetAttachProperty(DependencyObject dependencyObject)
14         {
15             return (bool)dependencyObject.GetValue(AttachProperty);
16         }
17         public static void SetAttachProperty(DependencyObject dependencyObject, bool value)
18         {
19             dependencyObject.SetValue(AttachProperty, value);
20         }
21         private static void OnAttachPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
22         {
23             PasswordBox passwordBox = d as PasswordBox;
24             if (passwordBox == null)
25             {
26                 return;
27             }
28             if ((bool)e.OldValue)
29             {
30                 passwordBox.PasswordChanged -= passwordchanged;
31             }
32             if ((bool)e.NewValue)
33             {
34                 passwordBox.PasswordChanged += passwordchanged;
35             }
36 
37         }
38 
39 
40         /// <summary>
41         /// 使用传统的.NET属性封装WPF依赖项属性。但典型的属性过程是检索或设置某个私有字段的值,而WPF属性的属性过程是使用在DependencyObject基类中定义的GetValue()和SetValue()方法
42         /// </summary>
43         /// <param name="dependencyObject"></param>
44         /// <returns></returns>
45         public static string GetPasswordContent(DependencyObject dependencyObject)
46         {
47             return (string)dependencyObject.GetValue(PasswordProperty);
48         }
49         public static void SetPasswordContent(DependencyObject dependencyObject,string value)
50         {
51             dependencyObject.SetValue(PasswordProperty, value);
52         }
53         /// <summary>
54         /// FrameworkPropertyMetadata的回调函数
55         /// </summary>
56         /// <param name="sender"></param>
57         /// <param name="e"></param>
58         private static void OnPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
59         {
60             PasswordBox passwordBox = sender as PasswordBox;
61             //-=:将 passwordchanged方法 从 passwordBox.PasswordChanged事件 中去除,+=是添加,也就是说当PasswordChanged事件发生时,passwordchanged方法也会跟着响应(如果已经+到事件中)
62             passwordBox.PasswordChanged -= passwordchanged;
63             string passwordvalue = (string)e.NewValue;
64             if (passwordBox != null && passwordBox.Password != passwordvalue)
65                 passwordBox.Password = passwordvalue;
66             passwordBox.PasswordChanged += passwordchanged;
67         }
68 
69         /// <summary>
70         /// Password改变后响应的方法
71         /// </summary>
72         /// <param name="sender"></param>
73         /// <param name="e"></param>
74         private static void passwordchanged(object sender, RoutedEventArgs e)
75         {
76             PasswordBox passwordBox = sender as PasswordBox;
77             SetPasswordContent(passwordBox, passwordBox.Password);
78         }
79     }

 

3.绑定

这里是用该属性去绑定某个值

还有一种绑定是,该属性作为源,写法如下,重点是加上括号

"{Binding ElementName=passwordbox, Path=(base:PasswordBoxHelper.PasswordContent)}"

这里的base是在xaml文件里面引用了命名空间,如下:(类PasswordBoxHelper就在Base文件夹下)

 

 

 

4.通知更改用的是prism中的方法,这里就不细说

 搞定

posted @ 2022-06-23 17:15  JustWantToStudy  阅读(477)  评论(0编辑  收藏  举报