Usercontrol Validation in ItemsControl

345 views Asked by At

I am working on a WPF application with MVVM, but am not using any of MVVM frameworks like MVVM light etc. I have created user controls that host atleast 8 to 9 controls and this is displayed in a ItemControl in the main window. The main window has save and close buttons. I have implemented Custom DataValidation, and if any of the controls has data validation, it disables the Save button. This works as expected in all the other controls but not working for ItemControl. The user control has data validations and displaying validation errors but I am not able to disable the save button. Any help will be really appreciated. Please let me know if you need additional details.

1

There are 1 answers

0
Vinit Sankhe On BEST ANSWER

To enable or disable button, you must consider using MVVM Commands.

They will allow you to bind a command to a button (such as Save) with a CanExecute() delegate function. This CanExecute() can check if any of the user controls on the ItemsControl is invalid (using the validity properties of the user controls) and then return false.

CanExecute() gets fired on almost every opportunity of rendering of the UI and hence it is kind of observable.