Data Validation Exception with Windows Community Toolkit MVVM

When using validations InView and we use MVVM from WKT 7.1.2 we get the following exception 

System.InvalidCastException: Unable to cast object of type 'System.ComponentModel.DataAnnotations.ValidationResult' to type 'System.String'.


The problem is that GetErrros return a enumeration of ValidationResult  defined in  System.ComponentModel.DataAnnotations

System.Collections.IEnumerable GetErrors(string propertyName)

It appears if you doing an explicit cast for the elements to string...  like this (string)obj.  Maybe it should be better to use ToString() or cater for  ValidationResult


For more info regarding the MVVM from the toolkit see

WindowsCommunityToolkit/ObservableValidator.cs at main · CommunityToolkit/WindowsCommunityToolkit (github.com)

Thanks

Stephanus


5 Replies

MA Mohanram Anbukkarasu Syncfusion Team November 29, 2021 12:25 PM UTC

Hi Stephanus, 

Thanks for contacting Syncfusion support.  

We regret to let you know that the provide details are insufficient to reproduce/validate the reported issue and it is impossible to find the exact cause for the issue without getting the code implementation details about the model class in which you have Implemented INotifyDataErrorInfo. Please revert to us with a simple sample to reproduce the issue in our end. It will be helpful for us to find the exact cause for the issue and to provide a prompt solution.  

Regards, 
Mohanram A. 



ST Stephanus December 2, 2021 11:21 AM UTC

Your DataGrid Validation does not work with Prism MVVM or Windows Community Toolkit MVVM.

According to this  Data Validation in UWP DataGrid control | Syncfusion I need to do my own implementation of  INotifyDataErrorInfo.  Those MVVM frameworks already did that plumbing and their implementations break with Syncfusion DataGrid.

It seems like your DataGrid expects a System.Collections.IEnumerable<string> from INotifyDataErrorInfo.GetErrors but the MVVM Frameworks return System.Collections.IEnumerable<ValidationResult>

When Validation fail the UWP app crash because it thinks the results are strings and not ValidationResult



VS Vijayarasan Sivanandham Syncfusion Team December 3, 2021 02:04 PM UTC

Hi Stephanus,

We regret for inconvenience.

We regret to inform you that we have tried to replicate the reported problem but unable to replicate the issue from our end. Can you please provide the issue reproducible sample and steps to replicate the issue.

It will be more helpful for us to find the exact cause for the issue and to provide a prompt solution.

Regards, 
Vijayarasan S 



SG Shehbaz Gil September 1, 2026 07:11 PM UTC

The issue does seem to come from treating the values returned by GetErrors() as strings. Since ObservableValidator.GetErrors() returns ValidationResult objects, the consuming code should handle the ValidationResult or extract its error message instead of using a direct string cast. This makes ToString() or accessing the appropriate message a more sensible approach.




GS Gowthamabalaji Sambath Syncfusion Team September 2, 2026 03:11 PM UTC

We have reviewed your query and were unable to reproduce the reported scenario on our end. The functionality works as expected when data validation is implemented using the INotifyDataErrorInfo interface and the GetErrors method. We created and tested the sample based on the MVVM pattern.
We also verified at the source level that the ErrorMessage property is of type string, and the value is assigned directly without performing any casting operation.
To help us reproduce the issue and provide an appropriate solution, please share the following details:
  • Could you please share how you have implemented the GetErrors method and the data validation logic?
  • Could you please confirm whether you are experiencing the reported issue in our provided sample?
  • Kindly modify the provided sample to demonstrate the reported scenario and share it with us.
This information will help us accurately reproduce the issue and assist you further.
Please find the sample demo in the attachment.
Regards,
Gowthamabalaji Sambath.

Attachment: SfDataGridValidation_fc3d466c.zip

Loader.
Up arrow icon