10 Replies
To check validation errors programmatically in C# when using the Model-View-ViewModel (MVVM) pattern, you would typically implement the INotifyDataErrorInfo interface for your ViewModel or Model classes. This interface allows you to provide asynchronous validation support and works well with data binding in WPF applications.
Here's a basic outline of the steps you would follow:
Implement INotifyDataErrorInfo: Your ViewModel or Model should implement the INotifyDataErrorInfo interface, which requires you to define an event named ErrorsChanged and methods like GetErrors and HasErrors.
Validate Properties: Within the property setters of your ViewModel or Model, you would perform the validation checks and add any errors to a collection that keeps track of current validation issues.
Notify Validation Errors: When validation errors occur, you should raise the ErrorsChanged event to notify the UI that there are new validation errors to be displayed.
Check Validation Errors Programmatically: You can check for validation errors programmatically by accessing the HasErrors property or by calling the GetErrors method and inspecting the returned values.
The XAML bindings and ValidationRules define how the errors are displayed, but they do not, by themselves, provide a method to check for errors programmatically. To do that, you would typically have to go through the ViewModel or the code-behind.
I'm still stuck when initializing usercontrol, the error is not validated so the button can be pressed. But after filling it in and then emptying it, it just updated.
Here is my code
AddCategoryVM.cs
And when using the number property, I can't change the default error message
@RedBear
@RedBear pls dont use AI to help people here
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.