I finally got a job!
Here's the solution:
In XAML, I added the following ValidationStep:
<my:UniqueNameSolidWoodRule CurrentCollection="{StaticResource CurrentSolidWoodCollection}" ValidationStep="CommittedValue"/>
Thus, I get 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 check, for example, HashCode . I can use to check if I am comparing the same object.
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); } }
source share