如何在TextBox下方显示错误信息且不超出Grid行范围
问题:TextBox错误信息显示导致Grid行高无法自动适配
我尝试为TextBox添加错误信息,希望将其显示在TextBox下方,且显示错误信息时Grid的行高能自动增加。当前错误信息超出了Grid的行范围,可查看附图了解情况。
我附上了左侧(TextBox部分)的View.xaml代码和样式style.xaml代码:
View.xaml
<!-- Other Code --> <Grid Grid.Row="0" Grid.Column="0" Margin="{StaticResource m-20}"> <Grid.Resources> <Style TargetType="Border" > <Setter Property="Padding" Value="15,5,5,5" /> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Border Grid.Row="0" Grid.Column="0"> <Label Content="Reg. No." Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"/> </Border> <Border Grid.Row="0" Grid.Column="1"> <TextBox VerticalAlignment="Top" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding Path=currentTrust.RegNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True}" MaxLength="{Binding Path=currentTrust.RegNo.MaxLength}" InputLanguageManager.InputLanguage="en-IN"/> </Border> <Border Grid.Row="1" Grid.Column="0"> <Label Content="Trust" VerticalAlignment="Center"/> </Border> <Border Grid.Row="1" Grid.Column="1"> <TextBox HorizontalAlignment="Stretch" TextWrapping="Wrap" VerticalAlignment="Center" Text="{Binding Path=currentTrust.TrustName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True,ValidatesOnExceptions=True}"/> </Border> <Border Grid.Row="2" Grid.Column="0"> <Label Content="Address" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"/> </Border> <Border Grid.Row="2" Grid.Column="1"> <TextBox HorizontalAlignment="Stretch" TextWrapping="Wrap" VerticalAlignment="Center" Text="{Binding Path=currentTrust.Address, Mode=TwoWay, UpdateSourceTrigger=LostFocus,IsAsync=True,ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/> </Border> <Border Grid.Row="3" Grid.Column="0"> <Label Content="Contact No" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"/> </Border> <Border Grid.Row="3" Grid.Column="1"> <TextBox HorizontalAlignment="Stretch" TextWrapping="Wrap" VerticalAlignment="Center" Text="{Binding Path=currentTrust.ContactNo, Mode=TwoWay, UpdateSourceTrigger=LostFocus,IsAsync=True,ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/> </Border> <Border Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"> <StackPanel Orientation="Horizontal"> <Button VerticalAlignment="Center" Style="{StaticResource AccentButton}" Content="{Binding Path=ButtonActionText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,IsAsync=True}" HorizontalContentAlignment="Center" Command="{Binding AddTrust}" /> <Button VerticalAlignment="Center" Content="Cancel" HorizontalContentAlignment="Center" Command="{Binding CancelTrust}" Margin="{StaticResource ml-5}"/> </StackPanel> </Border> </Grid> <!-- Other Code -->
style.xaml
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}"> <Setter Property="Padding" Value="{StaticResource p-5}"></Setter> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <StackPanel> <Border BorderThickness="2" BorderBrush="DarkRed" Margin="0 0 0 5"> <AdornedElementPlaceholder Name="placeholder" /> </Border> <Label Content="{Binding [0].ErrorContent}" Foreground="Red" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 0 0 5"/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Validation.HasError" Value="True"> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" /> </Trigger> </Style.Triggers> </Style>
我尝试过使用WrapPanel、ControlTemplate等方案,但都没有效果。
内容的提问来源于stack exchange,提问作者Naitik Kundalia
相关产品推荐
相关产品推荐

