• MVVM架构下wpf的密码框绑定


    背景:TextBox可以很轻松地对Text使用Binding,绑定ViewModel类里面的属性

            即:

            但是使用PasswordBox的密码框就不行了,因为没有Text这个属性,Password不是依赖属性

    那么就要自己实现一个PasswordBox的帮助类了

    第一步添加Helper类(创建在WPF用户控件库中)

    1. public class PasswordBindingHelper
    2. {
    3. public static readonly DependencyProperty PasswordProperty =
    4. DependencyProperty.RegisterAttached("Password",
    5. typeof(string), typeof(PasswordBindingHelper),
    6. new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
    7. public static readonly DependencyProperty AttachProperty =
    8. DependencyProperty.RegisterAttached("Attach",
    9. typeof(bool), typeof(PasswordBindingHelper), new PropertyMetadata(false, Attach));
    10. private static readonly DependencyProperty IsUpdatingProperty =
    11. DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
    12. typeof(PasswordBindingHelper));
    13. public static void SetAttach(DependencyObject dp, bool value)
    14. {
    15. dp.SetValue(AttachProperty, value);
    16. }
    17. public static bool GetAttach(DependencyObject dp)
    18. {
    19. return (bool)dp.GetValue(AttachProperty);
    20. }
    21. public static string GetPassword(DependencyObject dp)
    22. {
    23. return (string)dp.GetValue(PasswordProperty);
    24. }
    25. public static void SetPassword(DependencyObject dp, string value)
    26. {
    27. dp.SetValue(PasswordProperty, value);
    28. }
    29. private static bool GetIsUpdating(DependencyObject dp)
    30. {
    31. return (bool)dp.GetValue(IsUpdatingProperty);
    32. }
    33. private static void SetIsUpdating(DependencyObject dp, bool value)
    34. {
    35. dp.SetValue(IsUpdatingProperty, value);
    36. }
    37. private static void OnPasswordPropertyChanged(DependencyObject sender,
    38. DependencyPropertyChangedEventArgs e)
    39. {
    40. PasswordBox passwordBox = sender as PasswordBox;
    41. passwordBox.PasswordChanged -= PasswordChanged;
    42. if (!(bool)GetIsUpdating(passwordBox))
    43. {
    44. passwordBox.Password = (string)e.NewValue;
    45. }
    46. passwordBox.PasswordChanged += PasswordChanged;
    47. }
    48. private static void Attach(DependencyObject sender,
    49. DependencyPropertyChangedEventArgs e)
    50. {
    51. PasswordBox passwordBox = sender as PasswordBox;
    52. if (passwordBox == null)
    53. return;
    54. if ((bool)e.OldValue)
    55. {
    56. passwordBox.PasswordChanged -= PasswordChanged;
    57. }
    58. if ((bool)e.NewValue)
    59. {
    60. passwordBox.PasswordChanged += PasswordChanged;
    61. }
    62. }
    63. private static void PasswordChanged(object sender, RoutedEventArgs e)
    64. {
    65. PasswordBox passwordBox = sender as PasswordBox;
    66. SetIsUpdating(passwordBox, true);
    67. SetPassword(passwordBox, passwordBox.Password);
    68. SetIsUpdating(passwordBox, false);
    69. }
    70. }

    第二步:到xaml中使用帮助类添加PasswordBox的绑定就行了:

    local:PasswordBindingHelper.Password="{Binding LoginPwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

    这下就可以绑定VeiwModel类下面的LoginPwd了

    Attach是一个附加属性(Attached Property),它被用来激活或者禁用该 PasswordBindingHelper 的功能。

    参考:mvvmlight下passwordBox绑定的解决方法_azyrg88422的博客-CSDN博客

    好像上面的写复杂了,其实就是简单的附加属性就行

    帮助类中设置附加属性,附加属性没有指定是在哪个控件,想要在哪个地方用都行

    1. public class APPwd
    2. {
    3. public static string GetPassword(DependencyObject obj)
    4. {
    5. return (string)obj.GetValue(PasswordProperty);
    6. }
    7. public static void SetPassword(DependencyObject obj, string value)
    8. {
    9. obj.SetValue(PasswordProperty, value);
    10. }
    11. // Using a DependencyProperty as the backing store for Password. This enables animation, styling, binding, etc...
    12. public static readonly DependencyProperty PasswordProperty =
    13. DependencyProperty.RegisterAttached("Password", typeof(string), typeof(APPwd), new PropertyMetadata(null, new PropertyChangedCallback((s, e) =>
    14. {
    15. var pwd = s as PasswordBox;
    16. pwd.Password = e.NewValue.ToString();
    17. })));
    18. }

    用上附加属性的PasswordBox就可以用这个附加属性绑定ViewModel的内容了

    <PasswordBox local:APPwd.Password="{Binding Password}"/>

  • 相关阅读:
    小程序容器在一体化在线政务服务平台中的应用
    线程同步之互斥量
    【云开发】小程序端操作数据库详解
    多线程之线程安全集合类
    Java面经汇总
    347. 前 K 个高频元素——大顶堆、小顶堆
    文件搜索工具测试
    kettle学习--基础--01--介绍
    Maven——maven核心概念
    CMMI3认证和CMMI5认证有哪些不同
  • 原文地址:https://blog.csdn.net/weixin_46407807/article/details/132717586