title: Datagrid validation to prevent duplicate entry
body: <p>Using the code below I can trap an invalid cell entry. In this simple example of a grocery shopping list, the <code>GroceryItem.Name</code> just needs to be filled in.</p>
  
  <p>What I would like to do now is add the ability to validate that the entry doesn't already exist. If it does already exist then I'd like the same cell entry highlighted but with the appropriate message. So if the user enters "Eggs" again, the cell error message should be "Eggs is already on the list".</p>
  
  <p>The item view model shouldn't know about it's container view model, so where can I check for a duplicate entry while still at cell validation for the "Name" property?</p>
  
  <p><img src="https://i.stack.imgur.com/0UY2Z.jpg" alt="enter image description here"></p>
  
  <p>Item in the Collection</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class GroceryItem : ObservableObject, IDataErrorInfo
  {
  
      #region Properties
  
      /// &lt;summary&gt;The name of the grocery item.&lt;/summary&gt;
      public string Name
      {
          get { return _name; }
  
          set
          {
              _name = value;
              RaisePropertyChangedEvent("Name");
          }
      }
      private string _name;
  
      #endregion
  
      #region Implementation of IDataErrorInfo
  
      public string this[string columnName] {
          get {
              if (columnName == "Name") {
                  if (string.IsNullOrEmpty(Name)) 
                      return "The name of the item to buy must be entered";
              }
  
              return string.Empty;
          }
      }
  
      public string Error { get { ... } } 
  
      #endregion
  }
  </code></pre>
  
  <p>View Model holding the Collection</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MainWindowViewModel : ViewModelBase
  {
  
      /// &lt;summary&gt;A grocery list.&lt;/summary&gt;
      public ObservableCollection&lt;GroceryItem&gt; GroceryList
      {
          get { return _groceryList; }
  
          set
          {
              _groceryList = value;
              RaisePropertyChangedEvent("GroceryList");
          }
      }
      private ObservableCollection&lt;GroceryItem&gt; _groceryList;
  
      /// &lt;summary&gt;The currently-selected grocery item.&lt;/summary&gt;
      public GroceryItem SelectedItem { get; [UsedImplicitly] set; }
  
      void OnGroceryListChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
          // doing non-validation stuff 
      }
  }
  </code></pre>
  
  <p>View Binding for DataGrid</p>
  
  <pre><code>&lt;DataGrid 
      x:Name="MainGrid" 
      ItemsSource="{Binding GroceryList}" 
      SelectedItem="{Binding SelectedItem}" 
      ...            
      &gt;
      &lt;DataGrid.Resources&gt;
          &lt;Style TargetType="{x:Type DataGridCell}"&gt;
              &lt;Setter Property="TextBlock.ToolTip" 
              Value="{Binding Error}" /&gt;
          &lt;/Style&gt;
      &lt;/DataGrid.Resources&gt;
  
      &lt;DataGrid.Columns&gt;
          ...
          &lt;DataGridTextColumn Header="Item" Width="*" Binding="{Binding Name, ValidatesOnDataErrors=True}" IsReadOnly="false" /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>