title: How to use ValidatesOnDataErrors on a TextBox inside an ItemsControl
body: <p>I'm trying to have a TextBox's content be validated using IDataErrorInfo.  The source of the list below is a List and each item is display.  When i put <code>ValidatesOnDataErrors=True</code> in the Binding for the Text on the TextBox, it's not working as expected.   How do I do this?</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Trainings}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;StackPanel&gt;
                  &lt;StackPanel&gt;
                      &lt;TextBlock Text="{Binding MobileOperator}" /&gt;
                      &lt;TextBlock Text="{Binding LastUpdate}"/&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel&gt;
                      &lt;TextBlock Text="Number trained*" /&gt;                        
                      &lt;!-- ValidatesOnDataErrors doesn't work here--&gt;
                      &lt;TextBox 
                          Text="{Binding NumberTrained, 
                                         ValidatesOnDataErrors=True}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p><strong>Update:</strong>  Posting a stripped down version of Model, ViewModel, View and CodeBehind</p>
  
  <p>ViewModel and Model</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public class MyViewModel : IDataErrorInfo, INotifyPropertyChanged
  {
      public MyViewModel() 
      {
          Trainings = new List&lt;MyModel&gt;
          {
              new MyModel { NumberTrained = 5, MobileOperator = "MO 1", LastUpdate =         DateTime.Now },
              new MyModel { NumberTrained = 1, MobileOperator = "MO 2", LastUpdate = DateTime.Now },
          };
  
          OkButtonCommand = new ButtonCommand(OnClick);
      }
  
      private void OnClick()
      {
          PropertyChanged(this, new PropertyChangedEventArgs(""));
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      public ICommand OkButtonCommand { get; private set; }
      public List&lt;MyModel&gt; Trainings { get; private set; }
      public string Error { get { return null; } }
  
      public string this[string columnName]
      {
          get
          {
              string error = null;
              switch (columnName)
              {
                  case "NumberTrained":
                      error = "error from IDataErrorInfo";
                      break;
              }
              return error;
          }
      }
  }
  
  public class MyModel
  {
      public string MobileOperator { get; set; }
      public DateTime LastUpdate { get; set; }
      public int NumberTrained { get; set; }
  }
  
  public class ButtonCommand : ICommand
  {
      private Action _handler;
      public event EventHandler CanExecuteChanged;
  
      public ButtonCommand(Action handler) { _handler = handler; }
      public bool CanExecute(object parameter) { return true; }
      public void Execute(object parameter) { _handler(); }
  }
  </code></pre>
  
  <p>Code Behind</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public partial class MainPage : UserControl
  {
      public MainPage()
      {
          InitializeComponent();
  
          DataContext = new MyViewModel();
      }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;Canvas x:Name="LayoutRoot" Background="White"&gt;
      &lt;ItemsControl ItemsSource="{Binding Trainings}"&gt;
          &lt;ItemsControl.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel HorizontalAlignment="Center"&gt;
                      &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center"&gt;
                          &lt;TextBlock Text="{Binding MobileOperator}" Margin="15,15,0,0" FontWeight="Bold"/&gt;
                          &lt;TextBlock Text="{Binding LastUpdate, StringFormat=' - Last Updated: \{0:M/d/yy\}'}" 
                                     Margin="5,15,15,0" Foreground="Gray"/&gt;
                      &lt;/StackPanel&gt;
                      &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center"&gt;
                          &lt;TextBlock Text="Number trained*" /&gt;
                          &lt;TextBox Width="50" Height="20" 
                                   Text="{Binding NumberTrained, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/ItemsControl.ItemTemplate&gt;
      &lt;/ItemsControl&gt;
      &lt;Button Content="ok" Width="100" Height="20" Canvas.Left="248" Canvas.Top="207" Command="{Binding OkButtonCommand}"/&gt;
  &lt;/Canvas&gt;
  </code></pre>