简体   繁体   English

WPF PasswordBox验证

[英]WPF PasswordBox validation

I'm trying to add some validation rules to 2 PasswordBoxes. 我正在尝试向2个密码框添加一些验证规则。 Both must have more than 5 characters and, both passwords must match. 两者都必须超过5个字符,并且两个密码必须匹配。 I'm not currently using MVVM. 我当前未使用MVVM。

I figure I could check the password on the PasswordChanged event but I can't get the Invalid state to toggle on the boxes. 我认为我可以检查PasswordChanged事件上的密码,但无法在Invalid状态下切换框。

Does anyone have examples of something like this working? 有人有这样的例子吗?

If I'm understanding this correctly, all you need is this code within the PasswordChanged event of the second PasswordBox: 如果我正确地理解了这一点,那么您所需要的只是第二个PasswordBox的PasswordChanged事件中的以下代码:

    private void passwordBox2_PasswordChanged(object sender, RoutedEventArgs e)
    {
         if (passwordBox2.Password != passwordBox1.Password)
         {
             //Execute code to alert user passwords don't match here.
             passwordBox2.Background = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
         }
         else
         {
             /*You must make sure that whatever you do is reversed here;
              *all users will get the above "error" whilst typing in and you need to make sure
              *that it goes when they're right!*/
             passwordBox2.Background = new SolidColorBrush(Color.FromArgb(255,0,0,0));
          }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM