How to clear the contents of a PasswordBox when login fails without databinding?

前端 未结 1 1376
谎友^
谎友^ 2021-01-14 23:36

I have a wpf application and I am following the mvvm pattern carefully for reasons beyond my control. I do not want to databind to my PasswordBox for security reasons beyond

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-15 00:22

    You can create your attached DependencyProperty and use it as a XAML or in code. Example:

    Listing of PasswordBehaviors:

    public static class PasswordBehaviors
    {
        public static void SetIsClear(DependencyObject target, bool value)
        {
            target.SetValue(IsClearProperty, value);
        }
    
        public static readonly DependencyProperty IsClearProperty =
                                                  DependencyProperty.RegisterAttached("IsClear",
                                                  typeof(bool),
                                                  typeof(PasswordBehaviors),
                                                  new UIPropertyMetadata(false, OnIsClear));
    
        private static void OnIsClear(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is bool && ((bool)e.NewValue) == true)
            {
                PasswordBox MyPasswordBox = sender as PasswordBox;
    
                if (MyPasswordBox != null)
                {
                    MyPasswordBox.Clear();
                }
            }
        }
    }
    

    Using with EventTrigger:

    
        
            
                
                    
                        
                            True
                        
                    
                
            
        
    
    

    Using with DataTrigger (in Style/DataTemplate/etc):

    
        
    
    

    Using with Trigger (in Style):

    
        
    
    

    Using behind code:

    private void Clear_Click(object sender, RoutedEventArgs e)
    {
        PasswordBehaviors.SetIsClear(MyPasswordBox, true);
    }
    

    Copmlete example:

    XAML

    
    
    
        
            
                
                    
                        
                            
                                
                                    True
                                
                            
                        
                    
                
            
    
            
                
                    
                        
                            
                                
                                    False
                                
                            
                        
                    
                
            
        
    
        
    
        

    Code behind

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        //private void Clear_Click(object sender, RoutedEventArgs e)
        //{
        //    PasswordBehaviors.SetIsClear(MyPasswordBox, true);
        //}
    
        //private void ResetClear_Click(object sender, RoutedEventArgs e)
        //{
        //    PasswordBehaviors.SetIsClear(MyPasswordBox, false);
        //}
    }
    
    public static class PasswordBehaviors
    {
        public static void SetIsClear(DependencyObject target, bool value)
        {
            target.SetValue(IsClearProperty, value);
        }
    
        public static readonly DependencyProperty IsClearProperty =
                                                  DependencyProperty.RegisterAttached("IsClear",
                                                  typeof(bool),
                                                  typeof(PasswordBehaviors),
                                                  new UIPropertyMetadata(false, OnIsClear));
    
        private static void OnIsClear(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is bool && ((bool)e.NewValue) == true)
            {
                PasswordBox MyPasswordBox = sender as PasswordBox;
    
                if (MyPasswordBox != null)
                {
                    MyPasswordBox.Clear();
                }
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题