如何删除wpf顶部显示的验证显示错误框?

时间:2015-02-16 05:21:52

标签: wpf validation user-controls

在我的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>

1 个答案:

答案 0 :(得分:0)

查看XAML,看起来好像忘记了为每个输入字段向网格添加一行。所以你的名字&#39;文本块需要在Grid.Row = 0然后你的地址&#39;文本块需要为每个字段的Grid.Row = 1等等。对TextBox和ComboBox控件执行相同的操作,因此它们都是不同的Grid.Row。然后,您需要在Grid.RowDefinitions中添加一些额外的条目,以便RowDefinition条目的数量与显示所需的数量相匹配。