private void Window_Error(object sender, ValidationErrorEventArgs e) { var src = (DependencyObject)e.OriginalSource; switch (e.Action) { case ValidationErrorEventAction.Added: if (errorControls.Add(src)) { OKCommand.OnCanExecuteChanged(); ApplyCommand.OnCanExecuteChanged(); if (e.OriginalSource is FrameworkElement) { ((FrameworkElement)e.OriginalSource).Unloaded += Control_Unloaded; } } break; case ValidationErrorEventAction.Removed: if (!Validation.GetHasError((DependencyObject)e.OriginalSource)) { if (errorControls.Remove(src)) { OKCommand.OnCanExecuteChanged(); ApplyCommand.OnCanExecuteChanged(); if (e.OriginalSource is FrameworkElement) { ((FrameworkElement)e.OriginalSource).Unloaded -= Control_Unloaded; } } } break; } }
private void Control_Unloaded(object sender, RoutedEventArgs e) { if (sender is FrameworkElement) { ((FrameworkElement)sender).Unloaded -= Control_Unloaded; } errorControls.Remove((DependencyObject)sender); OKCommand.OnCanExecuteChanged(); ApplyCommand.OnCanExecuteChanged(); }