【问题标题】:Validation succeeds even when not valid即使无效,验证也会成功
【发布时间】:2017-11-06 08:53:11
【问题描述】:

我正在使用以下代码在保存之前检查错误,但是即使出现错误,“IsValid”也总是评估为真。

<DataGrid.RowValidationErrorTemplate>
     <ControlTemplate>
            <Grid  Margin="0,-2,0,-2" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
                  <Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}" Height="{TemplateBinding FontSize}" />
                      <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center"  />
                </Grid>
            </ControlTemplate>
        </DataGrid.RowValidationErrorTemplate>
    <DataGrid.RowValidationRules>
         <local:OrderItemValidationRule ValidationStep="UpdatedValue"/>
    </DataGrid.RowValidationRules>


OrderItemValidationRule rule = new OrderItemValidationRule();
foreach (DataGridRow dgr in orderItemDataGrid.GetDataGridRows())
{
   ValidationResult res = rule.Validate(dgr.BindingGroup, null);
   if (!res.IsValid)
   {
      MessageBox.Show("Cannot save, there are errors in the ORDER ITEMS.", "Error Message", MessageBoxButton.OK, MessageBoxImage.Error);
      dgr.BringIntoView();
      return false;
    }
}

验证规则:

public class OrderItemValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            OrderItem order = (value as BindingGroup).Items[0] as OrderItem;
            if (order.ProductID==0 && order.Product==null)
            {
                return new ValidationResult(false,"Please enter PRODUCT.");
            }
            if (order.Quantity==0)
            {
                return new ValidationResult(false, "Please enter QUANTITY.");
            }

             return ValidationResult.ValidResult;
        }
    }

【问题讨论】:

  • @dymanoid 请详细说明你评论的后半部分(我不是用Binding吗?)
  • 抱歉,我忽略了您将规则插入到DataGridRowValidationRules 中。
  • @dymanoid 你没有。最初的帖子中没有验证 xaml 代码。

标签: c# wpf validation datagrid


【解决方案1】:

GetDataGridRows() 是做什么的?这对我来说很好用:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    OrderItemValidationRule rule = new OrderItemValidationRule();
    foreach (object item in orderItemDataGrid.Items)
    {
        DataGridRow dgr = orderItemDataGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
        if (dgr != null)
        {
            ValidationResult res = rule.Validate(dgr.BindingGroup, null);
            if (!res.IsValid)
            {
                MessageBox.Show("Cannot save, there are errors in the ORDER ITEMS.", "Error Message", MessageBoxButton.OK, MessageBoxImage.Error);
                dgr.BringIntoView();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2017-10-15
    • 2016-12-14
    • 2014-02-17
    • 1970-01-01
    • 2013-01-16
    相关资源
    最近更新 更多