开发者

WPF (.net 3.5) ValidationRule , IDataErrorInfo

I'm trying f开发者_如何学编程igure out the best way to validate user input and I've been looking at ValidationRule and IDataErrorInfo. I have a VM and a model and I want to ensure a user does not enter alpha char's into multiple textbox's bound to doubles (or ints).

I'm running into 3 issues

1) When I use the ValidationRule the method returns a 'ValidationResult' but where does that go? Is it stored as property some where?

2) If I user IDataErrorInfo and enter some alpha char's it is never called (it is if numbers are entered) Is that expected? *

*(I thought maybe a value converter might help here but I feel like I'm mixing together two separate concepts)

3) Really what I want to do is do a validation at the end when a user clicks 'Save' and check all the values. So maybe using these two methods aren't what i need as per 1838300. Is that correct, these really are only for 'on the fly' validation?

My thought on point 3 was if the result of the ValidationRule was store somewhere I could check that for each control or where ever it is stored. Or if IDataErrorInfo was called I could manually store some Boolean for each control and check those.

Any thoughts or ideas?

Thanks!


There are a couple of things, you need to know:

When the type of the dependencyproperty is not the same as the underlying value - an automatic conversion is tried if no valueconverter is present.

This is all part of the normal binding engine. So, since your textbox input doesn't convert well to ints/doubles with alpha chars, an exception is thrown and will be continually thrown until you correct the value of the dependency property (here the TextBox's Text property) - the property setter of the underlying dataobject is never reached now.

You can verify this behaviour if you look in your output window for exceptions when you alter the text in the textbox. See this article to see how to properly implement Validation and IDataErrorInfo: link.


You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It shows how to use validation in WPF and how to control the Save button when validation errors exists.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜