Why my IDataErrorInfo interfaced model validates text but not default windows int validation errors?
2 Answers
The IDataErrorInfo interface validation requires that the input value that is going to be updated to data source should get converted to the source type correctly.So the empty string field cannot be converted to int value correctly, so the source data will retain the original value and the IDataErrorInfo cannot notify the error. You will need some custom validation rules here.

- 57
- 1
- 9
-
Exactly, I already added validation rules. But the problem with this, is because I used custom reusable user controls, the validation rules are on the textbox of that user control. In a single view, of course, I have many user controls. Do you know how can I extract the validation rule results from those user controls to the hosting view's viewmodel? (I can receive them to the 'hosting view', but to be able to have some logic (a list of errors etc) on those errors I need to have them in view model) – Doğaç Özen Dec 15 '18 at 12:12
-
@DoğaçÖzen This can be achieved by raising events at each usercontrol so that validation output could be sent through custom event args to the hosting view's viewmodel.If you could provide me a sample I can provide a better solution for that with demonstration. – AlphaWarrior Dec 15 '18 at 17:52
You can only set an int
property to a valid int
value and nothing else. It's not the resonsibility of a view model to validate that the view sets the property to an int
. The type is part of the view model's API and the contract between the view model and any consuming class including the view.
This kind of validation is performed by the WPF framework itself. If you want to customize it, you could use a validation rule in the view or the control. There is an example available here.
But you can't do anything in the view model (except from changing the type of the property to string
but this is generally a bad idea).

- 163,881
- 10
- 57
- 88