Validation Using MVVM Light in a Universal Windows App : <p>After done with setting up MVVM Light in a Universal Windows App application, I have the following structure, and I wonder what is the cleanest way to do validation in 2017 using UWP and mvvmlight to notify users with errors and possibly reset the textbox value when needed. The only trick is that the Textbox is part of a UserControl (cleaned up unnecessary xaml code for clarity) since it will be used multiple times. Also I added DataAnnotations and ValidationResult  for demonstration and not to suggest that this is the best way to do it or that it is working in any way so far.</p>

<p>The code works fine as far as binding and adding and removing values</p>

<ul>
<li><p><strong>ViewModel</strong></p>

<pre><code>using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Views;
using System;
using System.ComponentModel.DataAnnotations;

public class ValidationTestViewModel : ViewModelBase
 {
   private int _age;

  [Required(ErrorMessage = "Age is required")]
  [Range(1, 100, ErrorMessage = "Age should be between 1 to 100")]
  [CustomValidation(typeof(int), "ValidateAge")]
  public int Age
    {
      get { return _age; }
      set
      {
        if ((value &gt; 1) &amp;&amp; (value =&lt; 100))
            _age= value;
      }
    }

  public static ValidationResult ValidateAge(object value, ValidationContext validationContext)
   {
      return null;
   }
}
</code></pre></li>
<li><p><strong>View</strong></p>

<pre><code>&lt;Page
 x:Class="ValidationTest.Views.ValidationTestPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="using:ValidationTest.Views"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 DataContext="{Binding ValidationTestPageInstance, Source={StaticResource  Locator}}" 
xmlns:views="using:ValidationTest.Views"&gt;

     &lt;views:NumberEdit TextInControl="{Binding Age, Mode=TwoWay}" /&gt;

&lt;/Page&gt;
</code></pre></li>
<li><p><strong>UserControl</strong></p>

<pre><code>&lt;UserControl
 x:Class="ValidationTest.Views.Number"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="using:ValidationTest.Views"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 x:Name="userControl1"&gt;
  &lt;Grid&gt;

  &lt;TextBox x:Name="content" Text="{Binding TextInControl, ElementName=userControl1, Mode=TwoWay}"&gt;&lt;/TextBox&gt;
   &lt;/Grid&gt;

 &lt;/UserControl&gt;
</code></pre></li>
<li><p><strong>UserControl Code Behind</strong>:</p>

<pre><code>public partial class NumberEdit : UserControl
{
   public string TextInControl
      {
        get { return (string)GetValue(TextInControlProperty); }
        set {
              SetValue(TextInControlProperty, value);
            }
}

public static readonly DependencyProperty TextInControlProperty =
    DependencyProperty.Register("TextInControl", typeof(string),
                                   typeof(NumberEdit), new PropertyMetadata(null));

 }
</code></pre></li>
</ul>
