WPF TextBox with check loses ErrorTemplate

I have a problem very similar to these:

Problem with WPF validation (IDataErrorInfo) and tab .

TextBox with verification loses ErrorTemplate when changing tabs

AdornerDecoratordo the trick on the same instance Window , but when it reboots Windowand I switch to TabItem, the containing error TextBox ErrorTemplatewill no longer be displayed.

<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 repeat the check in the code on TabControl SelectionChanged, did not work.

Any idea?

+4
source share
2 answers

Connecting puzzle pieces

An AdornerLayer . AdornerLayer , , .

adorner - FrameworkElement, UIElement. AdornerLayer, , .

, adorner ( ) TextBox, TextBox.

(, ) GetAdornerLayer, AdornerLayer UIElement, .

TabItems AdornerLayer, adorner . 2 :

\ , DRapp:

<XAML for MyView1>
   <AdornerDecorator>
      ...
   </AdornerDecorator>
</close XAML for MyView1>

, , AdornerLayer AdornerDecorator TextBox ( ), . , AdornerDecorator , TextBox.

<XAML for MyView1>
    <Grid>

        ...

        <GroupBox>
            <AdornerDecorator>
                <Grid>

                    ...

                    <TextBox ... />
                </Grid>
            </AdornerDecorator>
        </GroupBox>
    </Grid>
</close XAML for MyView1>

\ , , ErrorTemplate , TextBox. AdornerLayer .

<UserControl.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="IsVisible" Value="true">
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border BorderBrush="Red" BorderThickness="1">
                                <AdornedElementPlaceholder/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
+3

WPF . , , , -, "MvView1" XAML.

...

<AdornerDecorator>
   <vw:MyView1 DataContext="{Binding}"/>
</AdornerDecorator>

...

<vw:MyView1 DataContext="{Binding}"/>

<XAML for MyView1>
   <AdornerDecorator>
      [wrapping is within the view... then the rest of the view...]
   </AdornerDecorator>
</close XAML for MyView1>

, , , , , , - . .

+2

Source: https://habr.com/ru/post/1663864/


All Articles