ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WPF中无法绑定PasswordBox的Password问题

2022-09-13 12:03:16  阅读:292  来源: 互联网

标签:obj DependencyProperty PasswordBox static WPF Password public 属性


由于PasswordBox的Password不是依赖属性,所以无法对其进行绑定。

这是需要通过添加附加属性,在附加属性中通过PasswordBox中可以用的依赖属性关联,实现Password绑定。

依赖属性:

-----MonitorPassword:关联PasswordBox中的PasswordChange事件。当PasswordChange事件发生时,修改AttachPassword属性。

-----AttachPassword:被绑定的附加属性

-----HasText:是否显示水印附加属性

 

附加属性类:

    public class PasswordProperties
    {
        #region 绑定PasswordBox.PasswordChange事件,一旦发生passwordChange事件,设置其余两个附件属性;
        public static bool GetMonitorPassword(DependencyObject obj)
        {
            return (bool)obj.GetValue(MonitorPasswordProperty);
        }

        public static void SetMonitorPassword(DependencyObject obj, bool value)
        {
            obj.SetValue(MonitorPasswordProperty, value);
        }

        // Using a DependencyProperty as the backing store for MonitorPassword.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MonitorPasswordProperty =
            DependencyProperty.RegisterAttached("MonitorPassword", typeof(bool), typeof(PasswordProperties), new PropertyMetadata(false, OnMonitorPasswordChanged));

        private static void OnMonitorPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox? p = d as PasswordBox;

            if (p == null) return;

            p.PasswordChanged -= P_PasswordChanged;

            if ((bool)e.NewValue)
            {
                SetHasText(p);
                p.PasswordChanged += P_PasswordChanged;
            }
        }
        private static void P_PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox? p = sender as PasswordBox;
            if(p == null) return;

            SetHasText(p);
            SetAttachPassword(p);
        }
        #endregion

        #region password的附件属性
        public static string GetAttachPassword(PasswordBox obj)
        {
            return (string)obj.GetValue(AttachPasswordProperty);
        }

        public static void SetAttachPassword(PasswordBox obj)
        {
            obj.SetValue(AttachPasswordProperty, obj.Password);
        }

        // Using a DependencyProperty as the backing store for AttachPassword.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AttachPasswordProperty =
            DependencyProperty.RegisterAttached("AttachPassword", typeof(string), typeof(PasswordProperties), new PropertyMetadata(0));
        #endregion

        #region password是否为空的附件属性,用于判断是否显示水印
        public static bool GetHasText(PasswordBox obj)
        {
            return (bool)obj.GetValue(HasTextProperty);
        }

        public static void SetHasText(PasswordBox obj)
        {
            obj.SetValue(HasTextProperty, obj.Password.Length > 0);
        }

        // Using a DependencyProperty as the backing store for HasText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HasTextProperty =
            DependencyProperty.RegisterAttached("HasText", typeof(bool), typeof(PasswordProperties), new PropertyMetadata(false));
        #endregion
    }

xmal:  绑定viewmodel中的Password属性

<PasswordBox local:PasswordProperties.MonitorPassword="True" local:PasswordProperties.AttachPassword="{Binding Password}" />

  

标签:obj,DependencyProperty,PasswordBox,static,WPF,Password,public,属性
来源: https://www.cnblogs.com/xuzhongjie/p/16688663.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有