【转】WPF中PasswordBox控件的Password属性的数据绑定

英文原文:http://www.wpftutorial.net/PasswordBox.html

中文原文:http://blog.csdn.net/oyi319/article/details/6551532

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

注:代码摘自:http://www.wpftutorial.net/PasswordBox.html

 1 using System.Windows;
 2 using System.Windows.Controls;
 3 namespace Oyi319.Helper
 4 {
 5     /// <summary>
 6     /// 为PasswordBox控件的Password增加绑定功能
 7     /// </summary>
 8     public static class PasswordBoxHelper
 9     {
10         public static readonly DependencyProperty PasswordProperty =
11             DependencyProperty.RegisterAttached("Password",
12             typeof(string), typeof(PasswordBoxHelper),
13             new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
14         public static readonly DependencyProperty AttachProperty =
15             DependencyProperty.RegisterAttached("Attach",
16             typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));
17         private static readonly DependencyProperty IsUpdatingProperty =
18            DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
19            typeof(PasswordBoxHelper));
20 
21         public static void SetAttach(DependencyObject dp, bool value)
22         {
23             dp.SetValue(AttachProperty, value);
24         }
25         public static bool GetAttach(DependencyObject dp)
26         {
27             return (bool)dp.GetValue(AttachProperty);
28         }
29         public static string GetPassword(DependencyObject dp)
30         {
31             return (string)dp.GetValue(PasswordProperty);
32         }
33         public static void SetPassword(DependencyObject dp, string value)
34         {
35             dp.SetValue(PasswordProperty, value);
36         }
37         private static bool GetIsUpdating(DependencyObject dp)
38         {
39             return (bool)dp.GetValue(IsUpdatingProperty);
40         }
41         private static void SetIsUpdating(DependencyObject dp, bool value)
42         {
43             dp.SetValue(IsUpdatingProperty, value);
44         }
45         private static void OnPasswordPropertyChanged(DependencyObject sender,
46             DependencyPropertyChangedEventArgs e)
47         {
48             PasswordBox passwordBox = sender as PasswordBox;
49             passwordBox.PasswordChanged -= PasswordChanged;
50             if (!(bool)GetIsUpdating(passwordBox))
51             {
52                 passwordBox.Password = (string)e.NewValue;
53             }
54             passwordBox.PasswordChanged += PasswordChanged;
55         }
56         private static void Attach(DependencyObject sender,
57             DependencyPropertyChangedEventArgs e)
58         {
59             PasswordBox passwordBox = sender as PasswordBox;
60             if (passwordBox == null)
61                 return;
62             if ((bool)e.OldValue)
63             {
64                 passwordBox.PasswordChanged -= PasswordChanged;
65             }
66             if ((bool)e.NewValue)
67             {
68                 passwordBox.PasswordChanged += PasswordChanged;
69             }
70         }
71         private static void PasswordChanged(object sender, RoutedEventArgs e)
72         {
73             PasswordBox passwordBox = sender as PasswordBox;
74             SetIsUpdating(passwordBox, true);
75             SetPassword(passwordBox, passwordBox.Password);
76             SetIsUpdating(passwordBox, false);
77         }
78     }
79 }

那么XAML绑定代码如下:

1 <PasswordBox Helper:PasswordBoxHelper.Attach="True" Helper:PasswordBoxHelper.Password="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />

MVVM模式XAML窗口声明代码如下:

 1 <Window
 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:Oyi319.Helper" 
 5     xmlns:ViewModels="clr-namespace:Oyi319.ViewModels"
 6     x:Class="Oyi319.Views.Login"
 7     x:Name="Window"
 8     Title="Login"
 9     FocusManager.FocusedElement="{Binding ElementName=txt_UserName}" >
10     <Window.DataContext>
11         <ViewModels:LoginViewModel />
12     </Window.DataContext>
13 </Window>

 

posted @ 2014-11-10 22:29  h82258652  阅读(7006)  评论(0编辑  收藏  举报