我有以下自定义Validation.ErrorTemplate:
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Label DockPanel.Dock="Bottom" Foreground="Red" Content="{Binding ErrorContent}" Margin="0,5,0,0" Background="LightGray"/>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
问题是,当ErrorContents比使用此样式的TextBox更宽时,红色边框将自身调整为ErrorContents而不是原始控件。如何强制边框始终是控件的大小,而不是ErrorContents?
答案 0 :(得分:1)
将Border的HorizontalAlignment从默认的Stretch更改为Left。这将允许它使用其所需的大小,这将是占位符的大小加上边框的大小,而不是被强制拉伸到DockPanel的宽度。
<DockPanel LastChildFill="True">
<Label DockPanel.Dock="Bottom" Foreground="Red" Content="{Binding ErrorContent}" Margin="0,5,0,0" Background="LightGray" />
<Border BorderBrush="Red" BorderThickness="2" HorizontalAlignment="Left">
<AdornedElementPlaceholder/>
</Border>
</DockPanel>