开发者

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

开发者 https://www.devze.com 2023-04-09 03:48 出处:网络
I have a textbox that is bound to a property that requires a value, ie: [Required(ErrorMessage = \"required value\")]

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 b开发者_JAVA技巧order 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?


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 );


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消