我有一个DataGrid
,绑定到键入的DataTable
,如下所示:
<DataGrid ItemsSource="{Binding Path=MyTypedDataTable}" ... />
此DataGrid
也有RowValidationRule
。一切正常,只有一种情况:当连续出现错误时我按下撤销按钮(MyTypedDataTable.RejectChanges()
);验证错误仍然存在,该行中的数据仍然相同。如果没有错误,RejectChanges()
正常工作。
如何使验证错误消失?提前感谢您的建议。
答案 0 :(得分:0)
我使用它,它适用于我的项目。
public class RowDataValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
BindingGroup group = (BindingGroup)value;
foreach (var item in group.Items)
{
DataRowView rowView = item as DataRowView;
DataRow row;
if (rowView != null)
row = rowView.Row;
else
row = item as DataRow;
if (row != null && row.HasErrors)
{
var errorColumns = row.GetColumnsInError();
StringBuilder sb = new StringBuilder();
foreach (var column in errorColumns)
{
sb.AppendLine(row.GetColumnError(column));
}
return new ValidationResult(false, sb.ToString());
}
}
return ValidationResult.ValidResult;
}
}
XAML
<DataGrid ItemsSource="{Binding Path=MyListItems, Mode=OneWay, ValidatesOnDataErrors=true, ValidatesOnExceptions=true}">
<DataGrid.RowValidationRules>
<Base:RowDataValidationRule />
</DataGrid.RowValidationRules>
</DataGrid>