WPF PasswordBox password数据绑定

本文介绍如何通过创建PasswordBoxHelper辅助类实现WPF中PasswordBox控件的Password属性与ViewModel层的数据双向绑定,适用于MVVM模式开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

原文链接:http://blog.youkuaiyun.com/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">

 

转载于:https://www.cnblogs.com/su-yang/p/7009956.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值