WPF Validation Verification Pattern Overlapping

I have a user control with a control pattern displaying validation errors, validation pattern:

<ControlTemplate x:Key="TextBoxPropertyValidationTemplate"> <StackPanel> <Border BorderBrush="Red" BorderThickness="1"> <AdornedElementPlaceholder x:Name="MyAdorner" /> </Border> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Grid.Column="0" MaxHeight="16" MaxWidth="16" Source="{Binding Source={StaticResource ValidationIcon}, Converter={StaticResource UriConverter}}" Margin="1" RenderOptions.BitmapScalingMode="HighQuality" VerticalAlignment="Center" HorizontalAlignment="Center" /> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" TextWrapping="Wrap" Grid.Column="1" FontSize="10" Foreground="Red" /> </Grid> </StackPanel> </ControlTemplate> 

And I can't get around a rather annoying problem that looks like this:

validation

I try to play with the fields in a user control, and the template also has the value Height = Auto, etc., but all this does not help. Any ideas anybody?

If this helps the main user control (which nests with validation) is in TabItem with AdornerDecorator.

Any help was appreciated.

+4
source share
2 answers

I would say this because your error message is on an AdornerLayer that is not participating in the same layout as your control. MSDN says "adorner rendering is independent of the rendering of the UIElement to which adorner is bound." and that’s why the message simply relied on everything.

You can put the error text in the original template, hide it based on Validation.HasError and include it in the layout process this way.

But changing the layout of the control may not be the best way out if a validation error occurs. You may consider providing more information in the tooltip.

+3
source

Alternatively, instead of using the ControlTemplate, you can put the TextBlock error message next to the TextBox and set its Text property, which binds the TextBox ErrorContent.

 <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBox x:Name="txtName" Grid.Row="0"> <TextBox.Text> <Binding Path="Name" NotifyOnValidationError="True" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <common:RequiredFieldValidationRule/> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> <TextBlock Grid.Row="1" Text="{Binding ElementName=txtName,Path=(Validation.Errors)[0].ErrorContent}" Visibility="{Binding ElementName=txtName,Path=Validation.HasError,Converter=...}" /> </Grid> 
+1
source

All Articles