WPF PasswordBox验证

时间:2010-07-16 17:37:23

标签: c# .net wpf

我正在尝试向2个PasswordBox添加一些验证规则。两者都必须超过5个字符,并且两个密码必须匹配。我目前没有使用MVVM。

我想我可以在PasswordChanged事件上检查密码,但我无法在框上切换无效状态。

有没有人有这种工作的例子?

1 个答案:

答案 0 :(得分:1)

如果我正确理解了这一点,那么你需要的就是第二个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));
          }
    }