I'm doing some validation on the DataSource of TextBox that's within an Expander and have found that once a validation error has been triggered, if I collapse the Expander, the red box stays where the TextBox would have been.
<Expander Header="Blah Blah Blah">
<TextBox Name="TextBox"
Validation.ErrorTemplate="{DynamicResource TextBoxErrorTemplate}"
Text="{Binding Path=Blah,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}" />
</Expander>
I've tried to get round this by binding the visibility of the Error Template to the Expander, however I think there's something 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've gone wrong with my binding, can someone put me back on track please? Alternatively does anyone know another solution to the ErrorTemplate still being visible on the collapse of an Expander?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…