سؤال

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