其他分享
首页 > 其他分享> > WPF中无法绑定PasswordBox的Password问题

WPF中无法绑定PasswordBox的Password问题

作者:互联网

由于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