How to bind to a PasswordBox in MVVM

前端 未结 30 2316
执念已碎
执念已碎 2020-11-22 11:50

I have come across a problem with binding to a PasswordBox. It seems it\'s a security risk but I am using the MVVM pattern so I wish to bypass this. I found som

30条回答
  •  醉话见心
    2020-11-22 12:24

    I posted a GIST here that is a bindable password box.

    using System.Windows;
    using System.Windows.Controls;
    
    namespace CustomControl
    {
        public class BindablePasswordBox : Decorator
        {
            /// 
            /// The password dependency property.
            /// 
            public static readonly DependencyProperty PasswordProperty;
    
            private bool isPreventCallback;
            private RoutedEventHandler savedCallback;
    
            /// 
            /// Static constructor to initialize the dependency properties.
            /// 
            static BindablePasswordBox()
            {
                PasswordProperty = DependencyProperty.Register(
                    "Password",
                    typeof(string),
                    typeof(BindablePasswordBox),
                    new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnPasswordPropertyChanged))
                );
            }
    
            /// 
            /// Saves the password changed callback and sets the child element to the password box.
            /// 
            public BindablePasswordBox()
            {
                savedCallback = HandlePasswordChanged;
    
                PasswordBox passwordBox = new PasswordBox();
                passwordBox.PasswordChanged += savedCallback;
                Child = passwordBox;
            }
    
            /// 
            /// The password dependency property.
            /// 
            public string Password
            {
                get { return GetValue(PasswordProperty) as string; }
                set { SetValue(PasswordProperty, value); }
            }
    
            /// 
            /// Handles changes to the password dependency property.
            /// 
            /// the dependency object
            /// the event args
            private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs eventArgs)
            {
                BindablePasswordBox bindablePasswordBox = (BindablePasswordBox) d;
                PasswordBox passwordBox = (PasswordBox) bindablePasswordBox.Child;
    
                if (bindablePasswordBox.isPreventCallback)
                {
                    return;
                }
    
                passwordBox.PasswordChanged -= bindablePasswordBox.savedCallback;
                passwordBox.Password = (eventArgs.NewValue != null) ? eventArgs.NewValue.ToString() : "";
                passwordBox.PasswordChanged += bindablePasswordBox.savedCallback;
            }
    
            /// 
            /// Handles the password changed event.
            /// 
            /// the sender
            /// the event args
            private void HandlePasswordChanged(object sender, RoutedEventArgs eventArgs)
            {
                PasswordBox passwordBox = (PasswordBox) sender;
    
                isPreventCallback = true;
                Password = passwordBox.Password;
                isPreventCallback = false;
            }
        }
    }
    

提交回复
热议问题