문제

I cannot get IDataErrorInfo to validate reference properties that have [Required] attribute set to them (or [ReferenceKey]) and that contains null as initial value. I am using WPF + EF 4.1. Validation is done through data annotations and IDataErrorInfo. For each property everything is working fine, except for the Reference properties that cannot contain null. This properties are This properties contain null as default value, and I cannot set it to any other value.

This in practice mean that this[string propertyName] is never executed for reference properties, and therefore UI doesn't catch any validation errors. I have inherited [Required] attribute just to see if this property is being validated at all, and it returns that it is non-valid, as it should, but IDataErrorInfo is totally unaware of this.

An example of reference property implementation in UI

<ComboBox DisplayMemberPath="Name"
          ItemsSource="{Binding UnitList}"
          SelectedValue="{Binding CurrentItem.UnitId, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
          SelectedValuePath="Id" />

What can I do to overcome this problem?

도움이 되었습니까?

해결책

The workaround I have found is to use use SelectedItem instead of SelectedValue.

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