我创建了一个 UserControl - 一个TextBox
工作得很好的 Labeled,除了验证模板。当出现错误时,验证控制模板会显示出来,但它会填满包括标签在内的整个空间。我只希望它和TextBox
. 如何解决这个问题?
这是xml:
<UserControl x:Class="Infrastructure.CustomControls.LabelTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="LTB">
<Grid HorizontalAlignment="{Binding}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="tbl"
FontFamily="{Binding}"
FontSize="{Binding}"
Text="{Binding ElementName=LTB, Path=LabelText}"
Height="{Binding ElementName=LTB, Path=LabelHeight}"
Width="{Binding ElementName=LTB, Path=LabelWidth}"
VerticalAlignment="Center"/>
<TextBox x:Name="tbx"
Grid.Column="1"
FontFamily="{Binding}"
FontSize="{Binding}"
IsReadOnly="{Binding ElementName=LTB, Path=IsReadOnly}"
MaxLength="{Binding ElementName=LTB, Path=TextMaxLength}"
Text="{Binding ElementName=LTB, Path=Text}"
Height="{Binding ElementName=LTB, Path=TextHeight}"
Width="{Binding ElementName=LTB, Path=TextWidth}"
VerticalAlignment="Center">
<Validation.ErrorTemplate>
<ControlTemplate>
<DockPanel LastChildFill="True"
ToolTip="{Binding ElementName=aep, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock DockPanel.Dock="Right"
Foreground="Red"
FontSize="14pt" Text="*"
Margin="-15,0,0,0"
FontWeight="Bold"/>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder Name="aep"/>
</Border>
</DockPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
</Grid>
</UserControl>