In WPF, when tab items are unloaded from the visual tree, the fact that they were marked as invalid is lost. Basically, when a validation error occurs, the user interface responds to an event in the validation stack and notes that the item is invalid. This marking is not reevaluated when an element is returned to the visual tree, unless the anchor is also re-evaluated (which usually does not happen if the user clicks on the tab element).
Define such a function somewhere (I put it in the static ValidationHelper class along with some other things):
public static void ReMarkInvalid( DependencyObject obj ) { if( Validation.GetHasError( obj ) ) { List<ValidationError> errors = new List<ValidationError>( Validation.GetErrors( obj ) ); foreach( ValidationError error in errors ) { Validation.ClearInvalid((BindingExpressionBase)error.BindingInError); Validation.MarkInvalid((BindingExpressionBase)error.BindingInError, error); } } for( int i = 0; i < VisualTreeHelper.GetChildrenCount( obj ); i++ ) { ReMarkInvalid( VisualTreeHelper.GetChild( obj, i ) ); } }
I think you can call this function in the TabControl Selected event, and it should have the desired effect. For example:.
private void TabControl_Selected(...) { ReMarkInvalid( tabControl ); }
If this does not work, you may need to do this with a lower dispatcher priority to ensure that the visual tree completes the download first. What would look like replacing ReMarkInvalid ... with:
Dispatcher.BeginInvoke( new Action( delegate() { ReMarkInvalid( tabControl ); } ), DispatcherPriority.Render );
Dana cartwright
source share