• 密码框密码不能绑定问题解决方案



    想要扩展该控件时发现PasswordBox是一个密封类,是不可继承的。

    故通过附加属性来实现该功能。

    声明了两个附加属性

    IsPasswordBindingEnabled:用来指示密码是否可用来绑定,当值发生改变时订阅或者取消订阅当Password改变时同步用于绑定的密码附加属性

    BindedPassword:用于绑定的密码,与密码同步,当值发生改变是时与Password同步

      /// <summary>
        /// 扩展密码框密码可用于绑定的帮助类
        /// </summary>
        public class BindedPasswordHelper
        {
            #region 指示密码框的密码是否可以绑定的附加属性
    
            public static bool GetIsPasswordBindingEnabled(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsPasswordBindingEnabledProperty);
            }
    
            public static void SetIsPasswordBindingEnabled(DependencyObject obj, bool value)
            {
                obj.SetValue(IsPasswordBindingEnabledProperty, value);
            }
    
            public static readonly DependencyProperty IsPasswordBindingEnabledProperty =
                DependencyProperty.RegisterAttached("IsPasswordBindingEnabled", typeof(bool),
                typeof(BindedPasswordHelper),
                new UIPropertyMetadata(false, OnIsPasswordBindingEnabledChanged));
    
            private static void OnIsPasswordBindingEnabledChanged(DependencyObject obj,
                                                                  DependencyPropertyChangedEventArgs e)
            {
                var passwordBox = obj as PasswordBox;
    
                if (passwordBox != null)
                {
                    passwordBox.PasswordChanged -= PasswordBoxPasswordChanged;
    
                    if ((bool)e.NewValue)
                    {
                        passwordBox.PasswordChanged += PasswordBoxPasswordChanged;
                    }
    
                }
            }
    
            //当密码发生改变时,更新用于绑定的密码
            private static void PasswordBoxPasswordChanged(object sender, RoutedEventArgs e)
            {
                var passwordBox = (PasswordBox)sender;
    
                if (!string.Equals(GetBindedPassword(passwordBox), passwordBox.Password))
                {
                    SetBindedPassword(passwordBox, passwordBox.Password);
                }
            }
    
            #endregion
    
            #region 用于绑定的密码的附加属性
    
            public static string GetBindedPassword(DependencyObject obj)
            {
                return (string)obj.GetValue(BindedPasswordProperty);
            }
    
    
            public static void SetBindedPassword(DependencyObject obj, string value)
            {
                obj.SetValue(BindedPasswordProperty, value);
            }
    
            public static readonly DependencyProperty BindedPasswordProperty =
                DependencyProperty.RegisterAttached("BindedPassword", typeof(string),
                typeof(BindedPasswordHelper),
                new UIPropertyMetadata(string.Empty, OnBindedPasswordChanged));
    
            //当被绑定的密码更新时,更新密码
            private static void OnBindedPasswordChanged(DependencyObject obj,
                                                        DependencyPropertyChangedEventArgs e)
            {
                var passwordBox = obj as PasswordBox;
                if (passwordBox != null && e.NewValue != null && passwordBox.Password != e.NewValue.ToString())
                {
                    passwordBox.Password = e.NewValue.ToString();
                }
            }
    
            #endregion
        }

    使用方法,这里要特别注意,与TextBox类似,一定要声明Mode已经UpdateSourceTrigger,不然绑定的值不会与后台的属性同步

        <PasswordBox Style="{StaticResource PasswordBoxStyle}"
                                 helper:BindedPasswordHelper.IsPasswordBindingEnabled="True"
                                 helper:BindedPasswordHelper.BindedPassword="{Binding Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
  • 相关阅读:
    这些特效对于学习前端我们很有用
    每周前端开源推荐第二期
    每周前端开源推荐第七期
    BeeFramework:以极客的方式开发你的应用
    node-webkit文档翻译#package.json
    android批量文件上传(android批量图片上传)
    VC多线程临界区
    【美妙的Python之三】Python 对象解析
    (转载)屌丝从毕业时的月入3000到三年后亿万身家的精彩励志之旅
    fedora 安装 pidgin-lwqq
  • 原文地址:https://www.cnblogs.com/XzcBlog/p/4887428.html
Copyright © 2020-2023  润新知