我創建了一個用戶控件 - 一個標記爲TextBox
,除了驗證模板以外,它工作得很好。當出現錯誤時,驗證控件模板將顯示出來,但它會填充整個空間,包括標籤。我只希望它和TextBox
一樣大。如何解決這個問題?已驗證的用戶控件中的文本框
這裏的XAML:
<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>
如果爲DockPanel設置Width =「{Binding ElementName = tbx,Path = ActualWidth}」,會發生什麼情況? – Bijan