views:

29

answers:

1

We're using RIA Services / Silverlight 4, and I'm binding a datagrid to something like Context.Foo.

I can see validation errors appearing in the datagrid's validation summary when users try to enter empty strings into mandatory fields and so forth, life is good.

However when I add a new item by calling something like Context.Foo.Add(new Foo) in the viewModel, the new row appears in the datagrid, but is never validated unless the user clicks on a cell.

Is there a way to ask the DataGrid is validate all items?

A: 

Rather than asking the DataGrid to validate the row for you, you will need to validate the object itself that the new row is bound to. You can use the Validator class to do this for you. For example, say your object is assigned to a variable named newRowObject, you can do the following:

List<ValidationResult> validationResults = new List<ValidationResult>();
ValidationContext validationContext = new ValidationContext(newRowObject, null, null);
bool isValid = Validator.TryValidateObject(newRowObject, validationContext, validationResults, true);

This should achieve what you are after (I emphasis should, simply because I didn't check it myself in an example before I wrote this).

Hope this helps...

Chris

Chris Anderson
Yep. Thanks for that Chris, I'm using code like that. The problem is that there doesn't seem to be any way to tell the DataGrid that this object has validation errors until the user clicks on one of its cells. I can add the items manually but the code isn't really mantainable. Sad that there isn't a UpdateValidation() method on the datagrid to loop through and check the objects. Thanks
ForeverDebugging