Search code examples
c#wpfidataerrorinfo

When changing back from another TabItem, all Data Validation (IDataError) infos were disappeading


A VS2017 solution illustrating this issue: https://github.com/ShannonZ/playground.git

IDataErrorInfo I implemented IDataErrorInfo in my ViewModel class. ViewModel only has two props X & Y and X+Y should not be larger than 10.

ValidatesOnDataErrors=True All TextBox binding to X/Y were set ValidatesOnDataErrors=True

What's QUIRK The two TextBox-es were red highlighted because X+Y=13>10 at beginning. Then if you change to the second TabItem and go back to the first one again, the two TextBox-es shows in a normal state. Even if you change the value manually (keep X+Y>10), the Error infos would not show again. enter image description here How to solve this problem?


Solution

  • After several days tracking and inspecting, I found that the Validation.Error informations are shown in the Adorner Layer, when you switch tabs, that layer is discarded. The way to solve it is simple: Add content inside an AdornerDecorator.

    <TabItem>
    <AdornerDecorator>
        <Grid>
    
        </Grid>
    </AdornerDecorator>