首页 > 代码库 > WPF PasswordBox password数据绑定
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 }
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数据绑定
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。