Java使用passwordbox,WPF中使用附加属性解决PasswordBox的数据绑定问题

WPF 中使用附加属性解决 PasswordBox 的数据绑定问题

1、前言

在 WPF 开发中 View 中的数据展示我们常通过 Binding 进行绑定。但是,使用 Binding 有一个前提:绑定的目标只能是依赖属性。 而 PasswordBox 控件中的 Password 并不是一个依赖属性,所以我们在使用 Password 时无法直接进行数据绑定。为了解决这个问题,我们就需要自己定义依赖属性。标题中的 “附加属性” 是依赖属性的一种特殊形式。

2、实现步骤

注:附加属性的定义方式:在 Visual Studio 中输入 propa ,然后按下两次 Tab 键即可。

2.1、定义一个 LoginPasswordBoxHelper 类,并在页面 xaml 代码中添加命名空间,该类用于辅助解决数据绑定问题。

2.2、在类中添加用于绑定的 Password 属性

public static class LoginPasswordBoxHelper{ public static string GetPassword(DependencyObject obj) { return (string)obj.GetValue(PasswordProperty); } public static void SetPassword(DependencyObject obj, string value) { obj.SetValue(PasswordProperty, value); } public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(LoginPasswordBoxHelper), new PropertyMetadata("")); }

这个时候就可以在页面的 xaml 中的 PasswordBox 中添加如下数据绑定了:

但是,这时候只是提供了一个属性给 PasswordBox 用于 Binding,输入内容后数据没有任何更改效果。

因为当在 PasswordBox 中填写密码时,没有启动对应的事件将密码 Changed 到后端 ViewModel 中的 Password 属性

这时就需要再建一个附加属性 IsPasswordBindingEnable,用于给 PasswordBox 的更改添加事件,并在事件中更改到 后端 ViewModel 中的 Password 属性。

2.3、添加附加属性 IsPasswordBindingEnable,用于给 PasswordBox 的添加更改事件

当 IsPasswordBindingEnable="True" 时,给 PasswordBox 的 PasswordChanged 事件添加处理程序PasswordBoxPasswordChanged;

PasswordBoxPasswordChanged 作用:当页面中 PasswordBox 输入的值发生改变时,通过 SetPassword 完成数据更改,从而实现完整的数据绑定功能。

public static bool GetIsPasswordBindingEnable(DependencyObject obj){ return (bool)obj.GetValue(IsPasswordBindingEnableProperty);}public static void SetIsPasswordBindingEnable(DependencyObject obj, bool value){ obj.SetValue(IsPasswordBindingEnableProperty, value);}public static readonly DependencyProperty IsPasswordBindingEnableProperty = DependencyProperty.RegisterAttached("IsPasswordBindingEnable", typeof(bool), typeof(LoginPasswordBoxHelper), new FrameworkPropertyMetadata(OnIsPasswordBindingEnabledChanged));private static void OnIsPasswordBindingEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e){ var passwordBox = obj as PasswordBox; if (passwordBox != null) { passwordBox.PasswordChanged -= PasswordBoxPasswordChanged; if ((bool)e.NewValue) { passwordBox.PasswordChanged += PasswordBoxPasswordChanged; } }}static void PasswordBoxPasswordChanged(object sender, RoutedEventArgs e){ var passwordBox = (PasswordBox)sender; if (!String.Equals(GetPassword(passwordBox), passwordBox.Password)) { SetPassword(passwordBox, passwordBox.Password); }}

3、完整代码

3.1、页面代码

Login.xaml

Login.xaml.cs

using PasswordBoxDemo.ViewModel;using System.Windows;namespace PasswordBoxDemo{ public partial class Login : Window { private MainViewModel resource; public Login() { InitializeComponent(); resource = new MainViewModel(); this.DataContext = resource; } }}

3.2、数据绑定辅助类 LoginPasswordBoxHelper

using System;using System.Windows;using System.Windows.Controls;namespace PasswordBoxDemo.ViewModel{ public static class LoginPasswordBoxHelper { public static string GetPassword(DependencyObject obj) { return (string)obj.GetValue(PasswordProperty); } public static void SetPassword(DependencyObject obj, string value) { obj.SetValue(PasswordProperty, value); } public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(LoginPasswordBoxHelper), new PropertyMetadata("")); public static bool GetIsPasswordBindingEnable(DependencyObject obj) { return (bool)obj.GetValue(IsPasswordBindingEnableProperty); } public static void SetIsPasswordBindingEnable(DependencyObject obj, bool value) { obj.SetValue(IsPasswordBindingEnableProperty, value); } public static readonly DependencyProperty.........

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值