Oct 162012

UPDATE: due to many requests, I have compiled the code below into a project that you can download from here: Validation.zip

To my big surprise the Microsoft team behind the WinRT XAML framework has decided not to include the built-in support for validation that exists in the WPF and Silverlight frameworks. Anyway, for anyone that needs an alternative, I have created a set of helper classes to make things easier.

 Implementing INotifyDataErrorInfo validation in Windows 8 WinRT XAML applications

The Validation static class contains two attached properties which work together for making the validation engine work:

  • Validation.Property contains the name of the bound property for which validation is done
  • Validation.ValidationPlaceholder contains a reference to a ContentControl which will be used for displaying any validation errors
  • The second part involved in the validation process is the implementation of the INotifyDataErrorInfo interface. I am providing here a ViewModel base class that contains both an implementation of the INotifyPropertyChanged and an implementation of INotifyDataErrorInfo which relies on the attributes available in the System.ComponentModel.DataAnnotations namespace for performing validation on the members:

    I have created a custom control that is used for displaying the validation errors. This control has nothing special, it just inherits from ContentControl and uses an ItemsControl for showing all validation errors belonging to a certain property:

    Finally, here is a very simple ViewModel and the associated XAML for it, which use the above framework. The sample code assumes that the DataContext of the XAML snippet is set to an instance of the SampleViewModel:

    • Pingback: Windows 8 Developer Links – 2012-10-17 | Dan Rigby

    • Pingback: Implementing INotifyDataErrorInfo validation in Windows 8 WinRT XAML applications

    • Daniel

      Hi, I’m having a stackoverflow in this part of your code:

      private void RaiseErrorsChanged(string propertyName)
      EventHandler handler = ErrorsChanged;
      if (handler != null)
      handler(this, new DataErrorsChangedEventArgs(propertyName));


      This results in an endless loop… any suggestions how to solve this?? Thanks!

      • Daniel

        Never mind, my wrong…

    • Daniel

      Hi, I really like your solution but I can’t get it to work… It must be something simple but I don’t see it.
      The problem is that it prints my view model’s class name on the screen instead of the error message…
      The only difference between mine and your code is that my property is on the SelectedItem-property of my ViewModel (which is a model). But for that I set:
      common:Validation.DataContext=”{Binding SelectedItem}”

      Do you have any idea why it’s printing my VM’s class name? I know some binding is trying to bind to it and then renders it as ToString, but I can’t find out where…
      I thought it would be the datatemplate of the validationresultplaceholder but replacing that binding by text didn’t change anything…

      Any help would be very much appreciated!

      • http://www.byteflux.me byteflux

        Hi Daniel,

        You are probably missing something inside your Views:ValidationResultPlaceholder style. The error messages are created inside the ValidateObject() method and they basically contain all the error messages generated by the validation attributes. The code builds a list of these errors, which in turn is passed to the ValidationResultPlaceholder control as the data context.

        This control will display each error as an entry inside it’s ItemsControl.

        If you cannot figure out your problem, drop me an email to contact [a t] byteflux.me. I will be happy to help you out with this issue.

    • Daniel

      Thanks, I finally solved it myself!
      For others who might run into the same problem: you’ll need to create the ValidationResultPlaceholder.cs and Generic.xaml via Add item -> Templated control.
      Don’t create a ResourceDictionary and class file yourself :)

    • ironmatze

      Thanks for your description.

      Due to the shortcomings of WinRT and other solutions like Prism for WinRT, I’ve written a comfortable and highly functional validation library. You can find it here: https://winrtxamlvalidation.codeplex.com/

      Kind regards,

      • byteflux

        Hi Matthias, your library looks very good. I also like that it uses the [CallerMemberNameAttribute] resulting in much cleaner and shorter notify property implementation.

    • Carlos Linares

      Can’t run the sample

      • http://www.byteflux.me/ Sebastian Negomireanu

        Hi Carlos,

        I will try to check the sample a bit later. It’s been a while since I’ve made it :).