如何删除 wpf 顶部显示的验证显示错误框?
How to remove Validation Display Error box shown on top in wpf?
在我的 wpf 用户控件中,我有一个带有错误验证框的文本框。它工作正常。但是当我减小屏幕尺寸时,错误框会出现在文本框旁边的下一个面板的顶部,当文本框和其他控件从面板下方移开时。如何让它正常工作?
这里是 xaml 代码:
<UserControl.Resources>
<BitmapImage x:Key="DefaultUser"
UriSource="/Capstone_ERP;component/Images/default-user-image.jpg" />
<ControlTemplate x:Key="LeftErrorTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding AdornedElement.(Validation.Errors).[0].ErrorContent, ElementName=ErrorAdorner}"
Foreground="#FFB83F3F"
TextAlignment="Right"
VerticalAlignment="Center"
FontFamily="Arial"
FontSize="11" />
<AdornedElementPlaceholder x:Name="ErrorAdorner">
<Border BorderBrush="LightCoral"
BorderThickness="1" />
</AdornedElementPlaceholder>
</StackPanel>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="27" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="165" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left"
Margin="24,24,0,0"
TextWrapping="Wrap"
Text="Name :"
VerticalAlignment="Top"
Height="24"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,70,0,0"
TextWrapping="Wrap"
Text="Address :"
VerticalAlignment="Top"
Height="20"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,176,0,0"
TextWrapping="Wrap"
Text="Contact No :"
VerticalAlignment="Top"
Height="20"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,218,0,0"
TextWrapping="Wrap"
Text="E-mail :"
VerticalAlignment="Top"
Height="20"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,260,0,0"
TextWrapping="Wrap"
Text="User Roll :"
VerticalAlignment="Top"
RenderTransformOrigin="1.793,-1.754"
Height="20"
Width="123" />
<TextBox Grid.Column="1"
Height="23"
Margin="9,22,0,0"
TextWrapping="Wrap"
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="418" />
<TextBox Grid.Column="1"
Height="91"
Margin="10,60,0,0"
TextWrapping="Wrap"
Text="{Binding Address, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="418"
AcceptsReturn="True"
ScrollViewer.CanContentScroll="True"
VerticalScrollBarVisibility="Auto" />
<TextBox Grid.Column="1"
Height="23"
Margin="9,173,0,0"
TextWrapping="Wrap"
Text="{Binding ContactNo, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="417" />
<TextBox Grid.Column="1"
Height="24"
Margin="10,215,0,0"
TextWrapping="Wrap"
Text="{Binding Email, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="417" />
<ComboBox Grid.Column="1"
Margin="9,254,0,0"
VerticalAlignment="Top"
Height="22"
ItemsSource="{Binding UserRollColloction,ValidatesOnDataErrors=True}"
SelectedItem="{Binding SelectedUserRoll}"
SelectedValuePath="Value"
DisplayMemberPath="Display"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
HorizontalAlignment="Left"
Width="417" />
</Grid>
查看 XAML 似乎您忘记了为每个输入字段在网格中添加一行。因此,您的 'name' 文本块需要位于 Grid.Row = 0 中,然后您的 'address' 文本块需要位于 Grid.Row = 1 中,依此类推。对您的 TextBox 和 ComboBox 控件执行相同的操作,因此它们各不相同 Grid.Row。然后您需要在 Grid.RowDefinitions 中添加一些额外的条目,以便 RowDefinition 条目的数量与您需要显示的数量相匹配。
在我的 wpf 用户控件中,我有一个带有错误验证框的文本框。它工作正常。但是当我减小屏幕尺寸时,错误框会出现在文本框旁边的下一个面板的顶部,当文本框和其他控件从面板下方移开时。如何让它正常工作?
这里是 xaml 代码:
<UserControl.Resources>
<BitmapImage x:Key="DefaultUser"
UriSource="/Capstone_ERP;component/Images/default-user-image.jpg" />
<ControlTemplate x:Key="LeftErrorTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding AdornedElement.(Validation.Errors).[0].ErrorContent, ElementName=ErrorAdorner}"
Foreground="#FFB83F3F"
TextAlignment="Right"
VerticalAlignment="Center"
FontFamily="Arial"
FontSize="11" />
<AdornedElementPlaceholder x:Name="ErrorAdorner">
<Border BorderBrush="LightCoral"
BorderThickness="1" />
</AdornedElementPlaceholder>
</StackPanel>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="27" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="165" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left"
Margin="24,24,0,0"
TextWrapping="Wrap"
Text="Name :"
VerticalAlignment="Top"
Height="24"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,70,0,0"
TextWrapping="Wrap"
Text="Address :"
VerticalAlignment="Top"
Height="20"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,176,0,0"
TextWrapping="Wrap"
Text="Contact No :"
VerticalAlignment="Top"
Height="20"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,218,0,0"
TextWrapping="Wrap"
Text="E-mail :"
VerticalAlignment="Top"
Height="20"
Width="123" />
<TextBlock HorizontalAlignment="Left"
Margin="24,260,0,0"
TextWrapping="Wrap"
Text="User Roll :"
VerticalAlignment="Top"
RenderTransformOrigin="1.793,-1.754"
Height="20"
Width="123" />
<TextBox Grid.Column="1"
Height="23"
Margin="9,22,0,0"
TextWrapping="Wrap"
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="418" />
<TextBox Grid.Column="1"
Height="91"
Margin="10,60,0,0"
TextWrapping="Wrap"
Text="{Binding Address, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="418"
AcceptsReturn="True"
ScrollViewer.CanContentScroll="True"
VerticalScrollBarVisibility="Auto" />
<TextBox Grid.Column="1"
Height="23"
Margin="9,173,0,0"
TextWrapping="Wrap"
Text="{Binding ContactNo, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="417" />
<TextBox Grid.Column="1"
Height="24"
Margin="10,215,0,0"
TextWrapping="Wrap"
Text="{Binding Email, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="417" />
<ComboBox Grid.Column="1"
Margin="9,254,0,0"
VerticalAlignment="Top"
Height="22"
ItemsSource="{Binding UserRollColloction,ValidatesOnDataErrors=True}"
SelectedItem="{Binding SelectedUserRoll}"
SelectedValuePath="Value"
DisplayMemberPath="Display"
Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"
HorizontalAlignment="Left"
Width="417" />
</Grid>
查看 XAML 似乎您忘记了为每个输入字段在网格中添加一行。因此,您的 'name' 文本块需要位于 Grid.Row = 0 中,然后您的 'address' 文本块需要位于 Grid.Row = 1 中,依此类推。对您的 TextBox 和 ComboBox 控件执行相同的操作,因此它们各不相同 Grid.Row。然后您需要在 Grid.RowDefinitions 中添加一些额外的条目,以便 RowDefinition 条目的数量与您需要显示的数量相匹配。