2010-07-16 69 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)); 
      } 
    }