Reputation: 47
I have a WinUI3 c# app that uses DataGrid from Windows Community Toolkit.
DataGrid is populated with Observable Collection, that consists of objects (which are basically rows from database, where property = column)
Users can edit this datatable. And for that of course I want to make sure that the input is correct. So that they can't enter letters in the cell, where there are should only be numbers, etc.
According to Windows Community Toolkit Documentation it should be done through INotifyDataErrorInfo in your DataModel or ViewModel. But it feels like it is too complex and I just need simple check.
So far, I could only come up with a plan, where I give columns in datagrid a certain tag, let's say "string", "int", "date". And then on "CellEditEdning" event check which tag the column has and based on that tag check the input, but I am not sure if that is a good idea and how to implement this.
So, I wanted to ask if there is any easier method to do it or should I do it all with INotifyDataErrorInfo, even if it is a simple check.
Upvotes: 0
Views: 1283
Reputation: 2146
INotifyDataErrorInfo
is just the mechanism to tell the UI that there are errors in your data.
It doesn't matter whether the validation rules are complex or simple, you still need a way to communicate that there are errors.
Sure you could use a different strategy, but that would probably be more work than you think because you'll likely have to implement some custom UI and a notification system. Whereas, as you pointed out from the documentation, DataGrid
already supports validation so half of the problem is already solved. Basically, the UI part (like display red cells) is already done. All you have to do is adjust your C# to have your objects implement INotifyDataErrorInfo
.
I recommend that you implement INotifyDataErrorInfo
instead of doing a custom implementation. It's likely the simplest solution to your problem.
If you don't want to implement INotifyDataErrorInfo
yourself, you could also use existing implementations that do it for you. You'll just have to learn how to set them up.
Here's an example of a MVVM framework that does implement INotifyDataErrorInfo
and that I used: https://github.com/nventive/Chinook.DynamicMvvm#chinookdynamicmvvm
Upvotes: 0