Wpf error template - the red frame is still visible when the expander crashes

I do some validation on the DataSource TextBox, which is inside the Expander, and find that after the validation error was triggered, if I hide the Expander, the red box will remain where the TextBox would be.

<Expander Header="Blah Blah Blah"> <TextBox Name="TextBox" Validation.ErrorTemplate="{DynamicResource TextBoxErrorTemplate}" Text="{Binding Path=Blah, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" /> </Expander> 

I tried to get around this by associating the visibility of the error pattern with the extender, but I think something is wrong with the binding.

 <local:NotVisibleConverter x:Key="NotVisibleConverter" /> <ControlTemplate x:Key="TextBoxErrorTemplate"> <DockPanel> <Border BorderBrush="Red" BorderThickness="2" Visibility="{Binding Path=IsExpanded, Converter={StaticResource NotVisibleConverter}, RelativeSource={RelativeSource AncestorType=Expander}}" > <AdornedElementPlaceholder Name="MyAdorner" /> </Border> </DockPanel> <ControlTemplate.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> 

I guess I did wrong with my binding, can someone get me back on track? Or does anyone know of another solution for ErrorTemplate that is still visible when Expander crashes?

+11
validation wpf binding controltemplate
Sep 24 '09 at 12:43
source share
3 answers

Instead of snapping, you can place an AdornerDecorator around the elements inside your expander. You see, the validation error pattern is placed on the adorner layer so that it appears on top of everything else. This is ultimately your problem. Although the text box is not visible because the expander is closed, the error pattern is still at the adorner level.

I believe you can fix this with the following xaml:

 <Expander Header="Blah Blah Blah"> <AdornerDecorator> <TextBox Name="TextBox" Validation.ErrorTemplate="{DynamicResource TextBoxErrorTemplate}" Text="{Binding Path=Blah, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" /> </AdornerDecorator> </Expander> 

This creates an adorner layer specifically for the expander. When the expander is closed, the AdornerDecorator also hides, and therefore everything should be on it.

+23
Sep 24 '09 at 13:37
source share

In general, debugging bindings can be done using:

  • Saving breakpoints in the converter (if you use the one you are)
  • Check the output area in Visual Studio for any debug warnings about invalid bindings

In the code you posted, I believe that this will happen because the Value property in Setter is not a dependency property and therefore cannot be related.

I will think about it and see if I can come up with something more useful.

+1
Sep 24 '09 at 13:40
source share

Pay attention to Donnelleโ€™s answer on How can I get rid of the red rectangle when my wpf snap check failed and the panel-panel is no longer visible? . This worked for me with an expander.

+1
Oct 08 '09 at 14:26
source share



All Articles