There is a number of posts to this forum regarding collection validation. Suggested solutions would typically rely on an entity to be aware of the collection it belongs to. The record is validated at the point of individual cell data entry. Sometimes it is preferable to validate the entire collection in one hit for performance reasons or due to the business requirements.
I have a MVVM implementation where an observable collection of entities is bound to XamDataGrid. Each Entity implements IDataErrorInfo. There is a button on a form that starts collection validation process. As a result of the validation Error property of each invalid entity is set to an appropriate error message. Is it possible without using code behind to force the grid to redisplay the collection and show record error indicator for each invalid entity?
Hello Boris,
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.
Hi Stefan
Thank you for the reply. This is exactly the behaviour I'm after. Just looking at your code I can't spot the difference between what I’ve tried and your sample. I'll have another look when I incorporate the code into my app and if the solution works (and I’m 99% confident that it would) I’ll mark your post as verified answer.
Thanks again. In our case your solution would make a huge difference for the end-user.
Cheers
Boris
I have copied the code from the feature browser and implement your addition code and everything seems to work as you want on my side. I also attached the sample, so If it doesn’t satisfies all your needs feel free to modify it, so it reproduces your behavior and send it back to me for further investigation.
Looking forward for your reply.
Hi Stephan
It is an excellent example indeed however it is not 100% what I'm after. Perhaps it will be a bit more clear if I illustrate it with some code.
Modify the example - Add a button to a form. In the code behind make Orders collection to be a member variable. Place the following code in button click event handler:
private void btnTest_Click(object sender, System.Windows.RoutedEventArgs e)
{
foreach (var o in orders)
o.Validate();
}
Implement Validate:
internal void Validate()
if (ProductName.Length > Quantity)
SetRecordDataError("My own Error", true );
What I would like to see is when I click Test button the collection of orders is validated and some of items become invalid with appropriate record level error displayed.
Unfortunately the grid is unaware that Error property has changed for some records and does not re-query Error property.
Raising NotifyPropertyChanged does not make any difference.
Thank you
Hello,
Thank you for your post. I have been looking into it and I can suggest you see the sample in the feature browser under xamDataGrid / Display / IDataErrorInfo Support.
Hope this helps you.