This project has moved. For the latest updates, please go here.

ViewModel IsValid property

Sep 13, 2012 at 6:59 AM

Hi,

I was wondering how to use the IsValid property of a ViewModel. In all the examples I can't find when it is actually set.

I want to use the property for returning if the Entity object validates. Using this to see if a command canexecute. (e.g. possible to Save the Entity).

Any idea how I can do this?

Coordinator
Sep 21, 2012 at 9:58 AM

The IsValid property of the ShellViewModel is set by the View via the ValidationHelper:

BookLibrary.Library.Presentation/Views/ShellWindow.xaml

waf:ValidationHelper.IsEnabled=”true” waf:ValidationHelper.IsValid=”{Binding IsValid, Mode=OneWayToSource}”

 
The EntityController uses this information to disable the Save command when the data is not valid.

BookLibrary.Library.Applications/Controllers/EntityController.cs – See CanSave method

Nov 30, 2012 at 7:49 AM

Hi,

Ok, I have worked it out. Now I know why it doesn't always work. It only validates visible controls. When you have a tabcontrol only the active tab gets validated.

Do you know what I have to do to check all tabs?

I think it has something to do with the ValidationHelper in System.Waf.Presentation, but I can't see it exactly.

Coordinator
Dec 4, 2012 at 6:49 PM

This is true. The WPF Validation infrastructure works only for visible elements.

You can see the same behavior in the BookLibrary sample application. Delete a book title so that a validation error occurs. The save button gets disabled. Switch to the Address book tab and see that the Save button gets enabled. If you press the save button now then a message box tells you that some validation errors still exists.

That is the way I have solved it in the BookLibrary application. I validate the domain layer before I save the objects into the database.

Maybe this approach works for you application as well.

Dec 11, 2012 at 2:28 AM

Hi,

I made a few changes to the BookLibrary so that if a validation error occurs while editing a cell, all other tab items on the active tab and also all inactive tabs are disabled.  Changes are made to the Controllers, ViewModels and Views. I am happy to post the details, if interested.

Actually, there seems to be a bug on the original sample in that if you enter an invalid input in a DataGrid cell and move to another row, without correcting it, and then delete the row of that cell, then the whole DataGrid becomes uneditable and you have to restart the program.  However, if you stay on the cell in question or move back to it and delete the cell's row, the DataGrid works fine.  One approach would be to disable the Remove button on the occurrence of a validation error; but this could be undesirable as you have to correct all the errors before you can delete the corresponding row.

Any ideas how to fix this?

Once again, a great framework!

Regards,

Sabba.

Dec 11, 2012 at 12:25 PM
Edited Dec 11, 2012 at 12:25 PM

Hi jbe,

What you say is not a solution, it is just basic behaviour to check the data in the domain. You never know if you forgot some validation tag somewhere in your view. This said, I'm thus one of those who actually validate the whole object in the domain layer.

Probably MS has good reasons, and maybe I start to see them, to only validate the visible properties. But, I really don't want my end-user pushing a save button when I know the view can't be saved.

I build a work-around, overriding the IsValid property of my viewmodel. When the property is set to true, I always recheck the whole model with the Validator.ValidateObject method. When my Error string is empty the property will be set when not the property will not be set and thus the propertychanged event will not be raised.

Coordinator
Dec 14, 2012 at 5:25 PM

@Sabba: Thank you for reporting this bug.

Working with the DataGrid is a bit tricky when a cell has a parser or validation error. Because then the cell doesn’t leave the editing mode. When the ItemsSource is manipulated during this state the DataGrid might become readonly which seems to be a bug of the DataGrid.

The simplest solution would be to disable the Remove command when the DataGrid is invalid. This is already done with the Add command but I missed it to do the same for the Remove command.

private bool CanAddNewBook() { return bookViewModel.IsValid; }

The more advanced implementation would be to check if the selected row to delete is the one with the validation error. If this is the case then we need first to call:

bookTable.CancelEdit(...)

I will fix this in one of the next versions of WAF.

Dec 19, 2012 at 5:39 PM

Hi JBE,

Thanks for your reply. I implemented your first suggestion and it works absolutely fine; but as mentioned in my previous comment, the user has to correct all the errors on a record then delete the record; not very user-friendly.

I tried your second suggestion (CancelEdit), similar to the CommitEdit event handler; but couldn't get it working: bookTable.CancelEdit(DataGridEditingUnit.Row).

I will wait till you fix the problem or if you give me a lead to try.

Thanks

Sabba.