How to validate a different property that the one changed using IDataErrorInfo with WPF?

StackOverflow https://stackoverflow.com/questions/9155410

  •  23-04-2021
  •  | 
  •  

문제

I'm using IDataErrorInfo to validate my viewmodels as it allows me to use a clear xaml sintax and it's pretty straightforward once you get it.

My question is how to validate one property when another one changes because the validation does not consist only on the value on the cell but depends on others. I see that the validation method is only called when the value of the property changes.

Thanks in advance.

도움이 되었습니까?

해결책

with IDataErrorInfo you can hook into

public string this[string columnName]

by overriding it, or writing your own.

in your case do a check that the columnName equals the property you are looking to validate and then either return a string with a value to represent a validation error, or null to say there was no error.

Also, to make sure it gets revalidated, when the dependent properties get updated, make sure you do a OnPropertyChanged for the main property

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top