WPF PasswordBox 添加附加屬性使之支持MVVM 數(shù)據(jù)綁定

眾所周知,WPF 的 PasswordBox 控件因?yàn)榘踩?,默認(rèn)不支持MVVM的雙向數(shù)據(jù)綁定,那么我們?cè)贛VVM開(kāi)發(fā)的過(guò)程中如何解決這個(gè)問(wèn)題呢?

添加附加屬性

新建PasswordBoxHelper.cs文件,添加如下的附加屬性:

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

namespace PasswordBoxControl.Helpers
{
    public class PasswordBoxHelper
    {
        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached(
            "Password", typeof(string), typeof(PasswordBoxHelper), new PropertyMetadata(string.Empty,OnPasswordPropertyChanged));

        private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox passwordBox=d as  PasswordBox;
            passwordBox.PasswordChanged -= PasswordChanged;
            if (!GetIsUpdating(passwordBox))
            {
                /*從Password往控件方向更新綁定值*/
                passwordBox.Password = e.NewValue.ToString();
            }

            passwordBox.PasswordChanged += PasswordChanged;
        }

        public static void SetPassword(DependencyObject element, string value)
        {
            element.SetValue(PasswordProperty, value);
        }

        public static string GetPassword(DependencyObject element)
        {
            return (string) element.GetValue(PasswordProperty);
        }

        public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached(
            "Attach", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false,Attach));

        private static void Attach(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is PasswordBox passwordBox))
            {
                return;
            }

            if ((bool)e.OldValue)
            {
                passwordBox.PasswordChanged -= PasswordChanged;
            }

            if ((bool)e.NewValue)
            {
                /*當(dāng)控件的值發(fā)生變化的時(shí)候,更新Password的值*/
                passwordBox.PasswordChanged += PasswordChanged;
            }
        }

        private static void PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox passwordBox=sender as PasswordBox;
            /*IsUpdating的作用類(lèi)似一把互斥鎖,因涉及到雙向綁定更新*/
            SetIsUpdating(passwordBox,true);
            SetPassword(passwordBox,passwordBox.Password);
            SetIsUpdating(passwordBox,false);
        }

        public static void SetAttach(DependencyObject element, bool value)
        {
            element.SetValue(AttachProperty, value);
        }

        public static bool GetAttach(DependencyObject element)
        {
            return (bool) element.GetValue(AttachProperty);
        }

        public static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached(
            "IsUpdating", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(default(bool)));

        public static void SetIsUpdating(DependencyObject element, bool value)
        {
            element.SetValue(IsUpdatingProperty, value);
        }

        public static bool GetIsUpdating(DependencyObject element)
        {
            return (bool) element.GetValue(IsUpdatingProperty);
        }
    }
}

如何使用呢?

在需要用的PasswordBox中添加上面寫(xiě)的附加屬性Password即可:

 <PasswordBox Helper:PasswordBoxHelper.Attach="True"
              Helper:PasswordBoxHelper.Password="{Binding Password,Mode=TwoWay}"/>

這下PasswordBox控件就可以像我們熟悉的MVVM 綁定的方式去使用了!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容