Search code examples
wpfprismprism-6

Is ValidatableBindableBase not implemented in Prism.Wpf? why?


Trying to incorporate ValidatableBindableBase for easy validation implementation, I've noticed that it's not available in Prism.Wpf.

It is available in Prism.Windows (Windows 10 UWP) however...

So could I have missed it (where is it then)?

Or is it really not implemented in WPF (then why)?


Solution

  • Validation in Prism.Wpf is done by implementing IDataErrorInfo or INotifyDataErrorInfo interfaces. An example:

    public abstract class DomainObject : INotifyPropertyChanged, INotifyDataErrorInfo
    {
        private ErrorsContainer<ValidationResult> errorsContainer =
                        new ErrorsContainer<ValidationResult>(
                           pn => this.RaiseErrorsChanged( pn ) );
    
        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    
        public bool HasErrors
        {
            get { return this.ErrorsContainer.HasErrors; }
        }
    
        public IEnumerable GetErrors( string propertyName )
        {
            return this.errorsContainer.GetErrors( propertyName );
        }
    
        protected void RaiseErrorsChanged( string propertyName )
        {
            var handler = this.ErrorsChanged;
            if (handler != null)
            {
                handler(this, new DataErrorsChangedEventArgs(propertyName) );
            }
        }
       ...
    }
    

    This is also explained in the documentation of Prism.

    So why doesn't UWP work that way? Because on UWP you don't have access to those interfaces and thus there was a need for ValidatableBindableBase and BindableValidator classes. If for some reason, you like this approach, nothing prevents you from taking the UWP classes and bring them to your WPF solution, all the code is open source.