我有一个名为UniqueNameSolidWoodRule验证DataGrid中重复的条目suclassed有效性规则。
下面的代码:
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属性,但unfortunnally我不出路来实现这一目标。