PasswordBox with MVVM

前端 未结 5 947
情深已故
情深已故 2020-12-16 11:59

Hi people stackoverflow. I\'m working with MVVM, I have ViewModel call UserViewModel with a Property Password. In the View have a control P

5条回答
  •  借酒劲吻你
    2020-12-16 12:16

    You can always write a control that wraps the Password and adds a dependency property for the Password property.

    I would just use code behind, but if you must you can do something like:

    public class BindablePasswordBox : Decorator
    {
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(BindablePasswordBox));
    
        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }
    
        public BindablePasswordBox()
        {
            Child = new PasswordBox();
            ((PasswordBox)Child).PasswordChanged += BindablePasswordBox_PasswordChanged;
        }
    
        void BindablePasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            Password = ((PasswordBox)Child).Password;
        }
    
    }
    

提交回复
热议问题