- private void GetErrors(StringBuilder sb, DependencyObject obj){
- foreach (object child in LogicalTreeHelper.GetChildren(obj)){
- TextBox element = child as TextBox;
- if (element == null)
- continue;
- if (Validation.GetHasError(element)){
- sb.Append(element.Text + " has errors: ");
- foreach (ValidationError error in Validation.GetErrors(element)){
- sb.Append(" " + error.ErrorContent.ToString());
- sb.Append(" ");
- }
- }
- }
- }
- private bool FromHasErrors(out string message){
- StringBuilder sb = new StringBuilder();
- GetErrors(sb, stackProductDetails);
- message = sb.ToString();
- return message != "";
- }
- private void cmdOK_Click(object sender, RoutedEventArgs e){
- string message;
- if (FromHasErrors(out message)){
- MessageBox.Show(message);
- }
- else{
- // ...
- }
- }
- }
http://blog.csdn.net/iHuoBaby/article/details/8576165