zl程序教程

您现在的位置是:首页 >  后端

当前栏目

MVVM 模式下 WPF Password 控件的数据绑定解析

WPF模式数据 解析 控件 绑定 password MVVM
2023-09-14 09:16:29 时间

WPF Password 控件的合理性

密码框控件是一种特殊类型的 TextBox,旨在输入密码。键入的字符将替换为星号。由于密码框包含合理的密码,因此不允许剪切,复制,撤消和重做命令

Password 的常用属性

更改密码字符

若要将星号字符替换为其他字符,请将 PasswordChar 属性设置为所需的字符。
<PasswordBox x:Name="passwordBox" PasswordChar="*" />

限制密码的长度

若要限制用户可以输入的密码的长度,请将该属性设置为允许的字符数。MaxLength
<PasswordBox x:Name="passwordBox" MaxLength="8" />

MVVM模式下 Password 控件的数据绑定

当您尝试对 PasswordBox 的密码属性进行数据绑定时,您将认识到您无法对其进行数据绑定。这样做的原因是,密码属性不受依赖项属性的支持。原因是出于安全原因,数据绑定密码不是一个好的设计,应该避免。但有时这种安全性不是必需的,那么您无法绑定到密码属性只会很麻烦。

  • WPF 控件是可以有附加属性的我们可以利用这一点来实现 Password 控件的数据绑定

这个类有个限制点,它在类库型的项目下是无法使用的

using System.Windows;
using System.Windows.Controls;

namespace Common
{
    /// <summary>
    /// Password 附加属性
    /// </summary>
    public class PasswordHelper
    {
        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper),
            new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));

        public static string GetPassword(DependencyObject d)
        {
            return (string)d.GetValue(PasswordProperty);
        }
        public static void SetPassword(DependencyObject d, string value)
        {
            d.SetValue(PasswordProperty, value);
        }

        public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(PasswordHelper),
            new PropertyMetadata(new PropertyChangedCallback(OnAttachChanged)));

        public static string GetAttach(DependencyObject d)
        {
            return (string)d.GetValue(AttachProperty);
        }
        public static void SetAttach(DependencyObject d, string value)
        {
            d.SetValue(AttachProperty, value);
        }

        static bool _isUpdating = false;
        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox pb = d as PasswordBox;
            pb.PasswordChanged -= Pb_PasswordChanged;
            if (!_isUpdating)
                (d as PasswordBox).Password = e.NewValue.ToString();
            pb.PasswordChanged += Pb_PasswordChanged;
        }

        private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox pb = d as PasswordBox;
            pb.PasswordChanged += Pb_PasswordChanged;
        }

        private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox pb = sender as PasswordBox;
            _isUpdating = true;
            SetPassword(pb, pb.Password);
            _isUpdating = false;
        }
    }
}

说到这里,不得不吐槽那些搬运的同学,很多人发出来的示例代码,根本就不能使用漏洞百出

在 xaml 文件中的使用及写法

  • 引入命名空间
<PasswordBox Width="240" password:PasswordHelper.Attach="True" password:PasswordHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>