Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

WPF TextBox with validation loses ErrorTemplate

I have an issue very similar to these:

Issue with WPF validation(IDataErrorInfo) and tab focusing

TextBox with validation loses ErrorTemplate on tab change

AdornerDecorator do the trick within the same instance of the Window, but when the Window is reloaded and I switch to the TabItem containing the TextBox in error, the ErrorTemplate won't show up anymore.

<Window x:Class="Views.MyWindowView">
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TabControl HorizontalAlignment="Stretch" 
                    Height="Auto"
                    VerticalAlignment="Top"
                    Width="Auto"
                    SelectionChanged="TabItemChanged"
                    Name="MyTabControl">

            <!-- Below, AdornerDecorator are added for the following reason:
                 the Validation.Error cues are painted in the Adorner Layer. 
                 When tabs are switched, that layer is discarded. -->

            <!-- The view 1 tab.-->
            <TabItem Header="{Resx tab1_Header}"
                     Name="Tbi1">
                <AdornerDecorator>
                    <vw:MyView1 DataContext="{Binding}"/>
                </AdornerDecorator>
            </TabItem>

            <!-- The view 2 tab.-->
            <TabItem Header="{Resx tab2_Header}"
                     Name="Tbi2">
                <AdornerDecorator>
                    <vw:MyView2 DataContext="{Binding}"/>
                </AdornerDecorator>
            </TabItem>
        </TabControl>

...

I tried to retrigger the validation in the code-behind on TabControl SelectionChanged, didn't work.

Any idea?

Answer*

Cancel
2
  • 1
    @CTZStef Some controls (e.g. Groupbox) seem to break the flow and require the AdornerDecorator to be placed within. As a quick check, try wrapping the TextBox.
    – Funk
    Commented Dec 20, 2016 at 10:21
  • @Funk wrapping the TextBox within an AdornerDecorator works; please create an answer and I will gladly accept it. If you could also add a few details about this magic, that would be great. Thanks!
    – CTZStef
    Commented Dec 20, 2016 at 14:37