我有一个带有 TextBox 的 XAML 窗口,而这个 TextBox 有一个 ErrorTemplate。
ErrorTemplate 如下所示,如您所见,我有一个 AdornedElementPlaceholder,后跟一个文本框,其文本字段绑定到 ErrorContent:
<ControlTemplate x:Key="ValidationErrorTemplateTextBlock" TargetType="{x:Type Control}">
<Border BorderBrush="Red" BorderThickness="1">
<StackPanel Orientation="Vertical">
<AdornedElementPlaceholder Name="AdornedElementPlaceholder" />
<TextBlock Text="{Binding ElementName=AdornedElementPlaceholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
FontSize="10"
Background="Red"
Foreground="White"
Padding="2" />
</StackPanel>
</Border>
</ControlTemplate>
<TextBox IsEnabled="{Binding SendMessage}"
Text="{Binding AutoMessageSubject, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource StyleBase}"
Validation.ErrorTemplate="{StaticResource ValidationErrorTemplateTextBlock}"
HorizontalAlignment="Stretch"
Grid.Row="3"
Grid.Column="1"
Grid.ColumnSpan="2" />
这很好用,除了一件事:TextBox 在 GridRow 内,Height="Auto"。该行根据文本框自行缩放,但是当出现 ErrorTemplate 时,底部有一个额外的 TextBox - GridRow 不会按比例放大以包含新的 TextBox,并且新的 TextBox 与其下方的元素重叠。
我该如何解决这个问题?