开发者

WPF validation order between IDataErrorInfo and exception

I have a control that has both ValidatesOnDataErrors = true and ValidatesOnExceptions = true. I also have something watching the Validation.Errors collection for this binding.

The user enters a value that's convertible (no exception) but invalid (IDataErrorInfo reports an error message). The error appears in the errors collection, and is displayed as expected.

T开发者_高级运维he user then enters a value that's not convertible (exception thrown). The IDataErrorInfo still reports the same error as before (since the model value didn't change).

In this latter case, I would prefer to see the exception error alone in the list, but would accept having both the exception and the data error (in any order).

However, what's actually happening is that only the data error ends up in the list, which ends up giving the user a misleading error message (since it's the error for the previous value they entered, not the current one).

Tracing it through internally, it appears that when the second user action occurs, the exception error is added to the list, then removed again and replaced with the data error. At no time do both errors appear in the list at the same time.

Any ideas how to get the desired behaviour out of this?

(Pulling back a step: what I'm trying to achieve is combining IDataErrorInfo validation [since it's more convenient for domain-level tests], but still paying attention to UI exceptions [otherwise it'd ignore the case when the user types in something totally silly]. I don't want to go to the extreme of making every VM property a string or other such nonsense.)


The ValidatesOnDataError and ValidatesOnExceptions are simply helper properties that add instances of DataErrorValidationRule and ExceptionValidationRule to the ValidationRules collection, respectively.

So you may be able to make the ExceptionValidationRule take precedence by explicitly defining the order like so:

<Binding Path="StartPrice" UpdateSourceTrigger="PropertyChanged">
    <Binding.ValidationRules>
        <ExceptionValidationRule />
        <DataErrorValidationRule />
    </Binding.ValidationRules>
</Binding>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜