Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation on TextBox: red border does not always appear on invalid results

I have a textbox that is bound to a property that requires a value, ie:

 [Required(ErrorMessage = "required value")]
 public string SomeText
 {
     //get set...
 }

And in my XAML, I have the following setup for my textbox:

 UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true, ValidatesOnExceptions=true

As expected, the red border appears when there is no value in the textbox, however when I select a different tab and then go back to the page with the invalid results, the red border no longer appears. It only reappears if I enter a valid result and then erase it.

How can I debug this? How can I find out what event causes the red border to appear?

like image 955
lost_bits1110 Avatar asked Sep 29 '11 17:09

lost_bits1110


2 Answers

In WPF when items on the tab get unloaded from the visual tree the fact that they were marked invalid is lost. Basically, when a validation error happens the UI responds to an event in the validation stack and marks the item invalid. This marking doesn't get re-evaluated when the item comes back into the visual tree unless the binding is also re-evaluated (which it usually isn't if the user clicks on a tab item).

Define a function like this somewhere (I put it in a 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's Selected event and it should have the desired effect. E.g.:

private void TabControl_Selected(...) 
{
    ReMarkInvalid( tabControl );
}

If that doesn't work you may need to do this at a lower Dispatcher priority to make sure the visual tree has finished loading first. Which would look like replacing ReMarkInvalid... with:

Dispatcher.BeginInvoke( new Action( delegate()
{
    ReMarkInvalid( tabControl );
} ), DispatcherPriority.Render );
like image 70
Dana Cartwright Avatar answered Nov 07 '22 09:11

Dana Cartwright


You can simply put the contents of your tab inside AdornerDecorator tags:

<TabControl>
    <TabItem>

        <AdornerDecorator>

            <ContentControl Content="{Binding TabItemViewModel}" />
            <Grid>
                <!-- Other Stuff -->
            </Grid>

        </AdornerDecorator>

    </TabItem>
</TabControl>

Update:

AdornerDecorator doesn't render borders on controls that are invisible (in unselected tabs). It just preserves borders across tabs once the borders have already been rendered.

However, the code put above by Dana Cartwright works fine. You just have to put a ClearInvalid before MarkInvalid as pointed out by lost_bits1110:

Validation.ClearInvalid((BindingExpressionBase)error.BindingInError);
Validation.MarkInvalid((BindingExpressionBase)error.BindingInError, error);
like image 40
Arko Avatar answered Nov 07 '22 10:11

Arko