WPF 验证错误文本溢出 [英] WPF Validation error text overflow
问题描述
你好,我对 xaml 很陌生,我真的不知道这样的错误叫什么.我有以下
和 以在我在后面的代码上创建的 TextBoxes 旁边显示验证错误.我遇到的问题是验证错误中的文本溢出网格,并且 ScrollViewer 不会像网格中的其他子项那样扩展它.
Hello I am very new to xaml and I really don't know what such a bug is called.
I have the following <ControlTemplate>
and <Style>
to display a validation error next to TextBoxes that I create on the code behind. The issue I'm having is the Text from the Validation Error overflow the Grid and the ScrollViewer doesn't expand for it as it does for other children in the Grid.
<UserControl.Resources>
<local:ValidationModels x:Key="validationModels" textBox_Text=" " />
<ControlTemplate x:Key="validationTemplate" >
<DockPanel Grid.Column="2">
<TextBlock Foreground="Red" FontSize="15" Text="Error" DockPanel.Dock="Right"></TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="InputControlErrors" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
然后我的 xaml 部分有
then I have the part of my xaml that has <ScrollViewer>
<Grid>
<StackPanel Orientation="Vertical">
<Label Content="NCR Assignment" FontSize="32" FontWeight="Bold" HorizontalAlignment="Center" Margin="16"/>
<ScrollViewer Height="314" Margin="48,0,52,0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible" >
<Grid Name="NCRGrid" RenderTransformOrigin="0.365,0.559">
<Grid.ColumnDefinitions >
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
</ScrollViewer>
</StackPanel>
我将 TextBlock 控件添加到网格的第一列,将 TextBox 控件添加到网格的第二列.
I add TextBlock controls to the first column and TextBox controls to the second column of the Grid.
我面临的问题是 ScrollViewer 扩展了 TextBlock 文本和 TextBox 字段(我可以水平滚动),但它不会扩展 ControlTemplate 中的验证错误文本,我看到它的唯一方法是最大化手动打开窗口.
The problem I'm facing is the ScrollViewer expands with the TextBlock text and TextBox field (I can scroll horizontally), but it doesn't expand for the validation error text from the ControlTemplate the only way I see it is by maximizing the window manually .
<ControlTemplate x:Key="validationTemplate" >
<DockPanel Grid.Column="2">
<TextBlock Foreground="Red" FontSize="15" Text="Error" DockPanel.Dock="Right"></TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
我在
推荐答案
这是设计使然.Validation.ErrorTemplate
将显示在装饰层上.装饰层中的元素呈现在其余视觉元素之上,并且在布局系统测量和排列控件时不会考虑它们.
This is by design. The Validation.ErrorTemplate
will be displayed on the adorner layer. Elements in the adorner layer are rendered on top of the rest of the visual elements and they will not be considered when the layout system is measuring and arranging the controls.
来自 MSDN:https://msdn.microsoft.com/en-us/library/system.windows.documents.adornerlayer(v=vs.110).aspx
放置在装饰层中的任何内容都将呈现在您设置的其余任何样式之上.换句话说,装饰器总是在视觉上位于顶部,不能使用 z-order 覆盖.
Anything placed in the adorner layer is rendered on top of the rest of any styles you have set. In other words, adorners are always visually on top and cannot be overridden using z-order.
这篇关于WPF 验证错误文本溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!