View to ViewModel Model Data Validation with no ICommands

Topics: Prism v4 - WPF 4
May 7, 2013 at 10:34 PM
I have a View class that contains a ItemsControl with a DataTemplate containing UserControls to edit the Item. But the View class doesn't have any <Button> controls only controls to edit the Item and the ViewModel class doesn't currently expose any ICommands for the View class to use, although with no <Button> controls I'm not sure how I would use a ICommand if it was available.

If I implement IDataErrorInfo in the Model class what is the best mechanism for the View to Notify the ViewModel of Validation Errors and/or Data Changes by the User?

Should the ViewModel hook into the Model PropertyChanged Events? In this case I have nested collections and hooking into the PropertyChanged Events seems awkward and could lead to classes getting rooted if I don't unhook the PropertyChanged hook.

Should the View class use the Binding SourceUpdated and Validation.Error event callbacks to call a method in the ViewModel?

Could the View class use a ViewModel ICommand if it doesn't have a <Button> control to bind the Command to?

Thanks in advance...
May 8, 2013 at 7:55 PM

Based on my understanding of your scenario, to implement Data Validation without using ICommands, you could implement one of the following approaches:

  • Implement IDataErrorInfo on your Model class and bind to its properties directly from your View or fire an event to tell the ViewModel that a Data Validation error ocurred and update the View accordingly.
  • Use Event Triggers on your View and define a method on your ViewModel to handle this event and do the Data Validation. This way, you can make the ViewModel to do Data Validation when an event from the Items of your ItemsControl is raised, like GotFocus and LostFocus.
For more information about IDataErrorInfo or Triggers, you can visit the following sections of the documentation:

Hope this helps,

Federico Martinez