用于唯一字段的WPF DataGrid验证规则 [英] Wpf datagrid validationrule for unique field
问题描述
我有一个名为UniqueNameSolidWoodRule的经过分类的ValidationRule,用于验证数据网格中的重复条目。
I've got a suclassed ValidationRule named UniqueNameSolidWoodRule to validate for duplicate entries in a datagrid.
这里是代码:
public class UniqueNameSolidWoodRule : ValidationRule
{
public CollectionViewSource CurrentCollection { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value != null)
{
ObservableCollection<SolidWood_VM> castedCollection = (ObservableCollection<SolidWood_VM>)CurrentCollection.Source;
foreach (SolidWood_VM swVM in castedCollection)
{
if (swVM.Designation == value.ToString())
{
return new ValidationResult(false, ResourcesManager.Instance.GetString("DuplicatedRecord"));
}
}
}
return new ValidationResult(true, null);
}
}
这是XAML代码段:
<DataGrid.Resources>
<CollectionViewSource x:Key="CurrentSolidWoodCollection" Source="{Binding Path=SolidWoods}" />
</DataGrid.Resources>
<DataGridTextColumn x:Name="Column2" Header="{x:Static p:Resources.Designation}" Width="auto">
<DataGridTextColumn.Binding>
<Binding Path="Designation" ValidatesOnDataErrors="True" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<my:UniqueNameSolidWoodRule CurrentCollection="{StaticResource CurrentSolidWoodCollection}"/>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
我的问题是有时该规则会将值与自己的行进行比较。我该如何预防?在我看来,我需要与该值关联的row.index属性进行比较,但不幸的是,我无法找到一种方法来实现此目的。
My problem is that sometimes this rule compares the value with its own row. How could I prevent this? It seems to me that I need a row.index property associated with the value to compare, but unfortunnally I can't figure a way out to achieve this.
推荐答案
我终于可以正常工作了!
I finally got things working!
这是解决方案:
在XAML中,我添加了以下ValidationStep:
In the XAML I added the following ValidationStep:
<my:UniqueNameSolidWoodRule CurrentCollection="{StaticResource CurrentSolidWoodCollection}" ValidationStep="CommittedValue"/>
这样,我得到了BindingExpression对象而不是字符串,作为覆盖的Validate的第一个参数方法,该方法为我提供了有关验证记录的更多信息,例如 HashCode ,我可以用来检查是否在比较同一对象。
This way I'm getting a BindingExpression object instead of a string as the first parameter of the overridden Validate method, which gives me much more information about the record to validate like the HashCode I can use to check if I'm comparing the same object.
这是更新的Validate方法:
Here's the updated Validate method:
public class UniqueNameSolidWoodRule : ValidationRule
{
public CollectionViewSource CurrentCollection { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value != null)
{
ObservableCollection<SolidWood_VM> castedCollection = (ObservableCollection<SolidWood_VM>)CurrentCollection.Source;
SolidWood_VM curValue = (SolidWood_VM)((BindingExpression)value).DataItem;
foreach (SolidWood_VM swVM in castedCollection)
{
if (curValue.GetHashCode() != swVM.GetHashCode() && swVM.Designation == curValue.Designation.ToString())
{
return new ValidationResult(false, ResourcesManager.Instance.GetString("DuplicatedRecord"));
}
}
}
return new ValidationResult(true, null);
}
}
这篇关于用于唯一字段的WPF DataGrid验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!