WPF PasswordBox.Password 的数据绑定

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

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

[c-sharp] view plain copy print ?
  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>  
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值