WPF PasswordBox password数据绑定

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF PasswordBox password数据绑定相关的知识,希望对你有一定的参考价值。

WPF的PasswordBox控件的Password属性不是依赖属性,无法直接进行数据绑定,为使其在MVVM模式中正常使用,可以为PasswordBox增加一个助手类,步骤如下:

原文链接:http://blog.csdn.net/ryb666666/article/details/7629767

1.新建一个PasswordBoxHelper 帮助类 

技术分享
 1  public static class PasswordBoxHelper
 2     {
 3         public static readonly DependencyProperty PasswordProperty =
 4             DependencyProperty.RegisterAttached("Password",
 5             typeof(string), typeof(PasswordBoxHelper),
 6             new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
 7         public static readonly DependencyProperty AttachProperty =
 8             DependencyProperty.RegisterAttached("Attach",
 9             typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));
10         private static readonly DependencyProperty IsUpdatingProperty =
11            DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
12            typeof(PasswordBoxHelper));
13 
14         public static void SetAttach(DependencyObject dp, bool value)
15         {
16             dp.SetValue(AttachProperty, value);
17         }
18         public static bool GetAttach(DependencyObject dp)
19         {
20             return (bool)dp.GetValue(AttachProperty);
21         }
22         public static string GetPassword(DependencyObject dp)
23         {
24             return (string)dp.GetValue(PasswordProperty);
25         }
26         public static void SetPassword(DependencyObject dp, string value)
27         {
28             dp.SetValue(PasswordProperty, value);
29         }
30         private static bool GetIsUpdating(DependencyObject dp)
31         {
32             return (bool)dp.GetValue(IsUpdatingProperty);
33         }
34         private static void SetIsUpdating(DependencyObject dp, bool value)
35         {
36             dp.SetValue(IsUpdatingProperty, value);
37         }
38         private static void OnPasswordPropertyChanged(DependencyObject sender,
39             DependencyPropertyChangedEventArgs e)
40         {
41             PasswordBox passwordBox = sender as PasswordBox;
42             passwordBox.PasswordChanged -= PasswordChanged;
43             if (!(bool)GetIsUpdating(passwordBox))
44             {
45                 passwordBox.Password = (string)e.NewValue;
46             }
47             passwordBox.PasswordChanged += PasswordChanged;
48         }
49         private static void Attach(DependencyObject sender,
50             DependencyPropertyChangedEventArgs e)
51         {
52             PasswordBox passwordBox = sender as PasswordBox;
53             if (passwordBox == null)
54                 return;
55             if ((bool)e.OldValue)
56             {
57                 passwordBox.PasswordChanged -= PasswordChanged;
58             }
59             if ((bool)e.NewValue)
60             {
61                 passwordBox.PasswordChanged += PasswordChanged;
62             }
63         }
64         private static void PasswordChanged(object sender, RoutedEventArgs e)
65         {
66             PasswordBox passwordBox = sender as PasswordBox;
67             SetIsUpdating(passwordBox, true);
68             SetPassword(passwordBox, passwordBox.Password);
69             SetIsUpdating(passwordBox, false);
70         }
71     }
View Code

2.在View层XAML绑定

1  <PasswordBox x:Name="passwordBox"  PasswordChar="*" Helper:PasswordBoxHelper.Attach="True"
2              Helper:PasswordBoxHelper.Password="{Binding Path=Pwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
1 <Window x:Class="HR_Test01.View.LoginView"
2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4         xmlns:Helper="clr-namespace:HR_Test01.Model"        
5         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7         xmlns:local="clr-namespace:HR_Test01.View"
8         mc:Ignorable="d"  AllowsTransparency="True" WindowStyle="None"  MouseLeftButtonDown="Window_MouseLeftButtonDown" 
9         Title="登录界面" Height="250" Width="400" WindowStartupLocation="CenterScreen">

 

以上是关于WPF PasswordBox password数据绑定的主要内容,如果未能解决你的问题,请参考以下文章

WPF -- PasswordBox数据绑定方法

为啥WPF里面的PasswordBox的Password属性不是依赖属性?

WPF的TextBox以及PasswordBox显示水印文字

WPF -- PasswordBox数据绑定方法

WPF PasswordBox不支持绑定解决方法

WPF 绑定密码