Advertisement
Intermediate .NET MAUI, Advanced Data Binding & Collections

How do you implement input validation with data binding?

Short answer

Expose validation state on the view model (per-field error properties or INotifyDataErrorInfo), bind UI to show errors (DataTrigger or an error label), and disable submit via a command's CanExecute.

Save .NET MAUI mock interview
Advertisement
AModel answer
Expose validation state on the view model (per-field error properties or INotifyDataErrorInfo), bind UI to show errors (DataTrigger or an error label), and disable submit via a command's CanExecute. The CommunityToolkit and behaviors package also offer validation behaviors for common rules.
public bool HasEmailError => string.IsNullOrEmpty(Email) ||
    !Email.Contains('@');
// Submit command CanExecute returns !HasEmailError && ...
Real-world example
A signup form highlights invalid fields and keeps the Create button disabled until all validations pass.
Likely follow-ups
  • What is INotifyDataErrorInfo? | How do you disable submit on errors?
Related topics