How to Build Real-Time WinRT XAML Validation

Written by

in

How to Build Real-Time WinRT XAML Validation Real-time input validation improves user experience by catching errors immediately [1]. WinRT XAML does not include the built-in IDataErrorInfo interface found in WPF [1]. You can build a robust, reactive validation system using data binding and the INotifyDataErrorInfo interface [1, 2].

Here is how to implement real-time validation in your WinRT applications. 1. Implement INotifyDataErrorInfo on a Base ViewModel

The INotifyDataErrorInfo interface requires you to track errors per property and notify the UI when errors change [2]. Create a reusable base class to handle this logic.

using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; public class ViewModelBase : INotifyPropertyChanged, INotifyDataErrorInfo { private readonly Dictionary> _errors = new(); public event PropertyChangedEventHandler PropertyChanged; public event EventHandler ErrorsChanged; public bool HasErrors => _errors.Any(); public IEnumerable GetErrors(string propertyName) { if (string.IsNullOrEmpty(propertyName) || !_errors.ContainsKey(propertyName)) { return Enumerable.Empty(); } return _errors[propertyName]; } protected void RaisePropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected void ClearErrors(string propertyName) { if (_errors.ContainsKey(propertyName)) { _errors.Remove(propertyName); OnErrorsChanged(propertyName); } } protected void AddError(string propertyName, string error) { if (!_errors.ContainsKey(propertyName)) { _errors[propertyName] = new List(); } if (!_errors[propertyName].Contains(error)) { _errors[propertyName].Add(error); OnErrorsChanged(propertyName); } } protected void OnErrorsChanged(string propertyName) { ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); RaisePropertyChanged(nameof(HasErrors)); } } Use code with caution. 2. Trigger Validation in Property Setters

Perform data checks directly inside your ViewModel properties. Call validation logic every time the user types or changes a value.

public class RegistrationViewModel : ViewModelBase { private string _email; public string Email { get => _email; set { if (_email != value) { _email = value; RaisePropertyChanged(nameof(Email)); ValidateEmail(value); } } } private void ValidateEmail(string value) { ClearErrors(nameof(Email)); if (string.IsNullOrWhiteSpace(value)) { AddError(nameof(Email), “Email address is required.”); return; } if (!value.Contains(“@”) || !value.Contains(“.”)) { AddError(nameof(Email), “Enter a valid email address.”); } } } Use code with caution. 3. Configure the XAML Data Binding

To make validation occur in real time as the user types, set the UpdateSourceTrigger property to PropertyChanged. Enable the ValidatesOnExceptions or custom binding flags if your framework profile requires it.

Use code with caution. 4. Display Error Messages in the UI

WinRT XAML does not always style validation errors automatically. Use a simple ControlTemplate or bind a secondary control directly to the error state to show validation text to your users.

Use code with caution.

(Note: If utilizing custom frameworks like Prism or Template 10, use their built-in behaviors to automate the error text display). 5. Disable Form Submission on Error

Bind your submission button’s operational state directly to the HasErrors property of your ViewModel.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *