2010-07-16 66 views
0

我试图给2个密码框添加一些验证规则。两者都必须有超过5个字符,且两个密码必须匹配。我目前没有使用MVVM。WPF密码框验证

我想我可以检查PasswordChanged事件的密码,但我无法得到无效的状态来切换框。

有没有人有像这样的工作的例子?

回答

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)); 
      } 
    }