title: IDataErrorInfo on ObservableCollection
body: <p>I have a viewmodel that implements IDataError. In the viewmodel I have an ObservableCollection. The ObservableCollection populates a datagrid in my view:</p>
  
  <pre><code>    // the list that populates the datagrid
      public ObservableCollection&lt;ProjectExpenseItemsDto&gt; ListOfProjectExpenseItems
      {
          get { return listOfProjectExpenseItems; }
          set
          {
              if (listOfProjectExpenseItems != value)
              {
                  listOfProjectExpenseItems = value;
                  NotifyPropertyChanged("ListOfProjectExpenseItems");
              }
          }
      }
  </code></pre>
  
  <p>I also have a property that represents the selected item in the datagrid (it is based off a Dto):</p>
  
  <pre><code>    // the selected row in the datagrid
      public ProjectExpenseItemsDto SelectedProjectExpenseItem
      {
          get { return selectedProjectExpenseItem; }
          set
          {
              if (selectedProjectExpenseItem != value)
              {
                  selectedProjectExpenseItem = value;
                  NotifyPropertyChanged("SelectedProjectExpenseItem");
              }
          }
      }
  </code></pre>
  
  <p>Here is the Dto:</p>
  
  <pre><code>namespace ProjectExpense.Model.Dto
  {
      [DataContract]
      public class ProjectExpenseItemsDto
      {
          [DataMember]
          public int RowID { get; set; }
          [DataMember]
          public int ProjectExpenseID { get; set; }
          [DataMember]
          public string ItemNumber { get; set; }
          [DataMember]
          public string ItemDescription { get; set; }
          [DataMember]
          public decimal ItemUnitPrice { get; set; }
          [DataMember]
          public decimal ItemQty { get; set; }
          [DataMember]
          public string SupplierName { get; set; }
          [DataMember]
          public DateTime CreateDate { get; set; }
      }
  }
  </code></pre>
  
  <p>I want to use IDataError to validate values in the selected row of the datagrid (SelectedProjectExpenseItem), but since my grid is bound to the ObservableCollection, I don't have any properties in my viewmodel; therefore, I can only use IDataError against the ObservableCollection, not the individual items in the collection, which doesn't help me because I have know way to see "inside" the collection. I cannot use IDataError for my SelectedProjectExpenseItem either. For example:</p>
  
  <pre><code>    string IDataErrorInfo.this[string propertyName]
      {
          get
          {
              string result = string.Empty;
              propertyName = propertyName ?? string.Empty;
              if (propertyName == string.Empty || propertyName == "ItemNumber")
              {
                  if (string.IsNullOrEmpty(SelectedProjectExpenseItem.ItemNumber))
                  {
                      result = "Name cannot be blank!";
                  }
              }
              return result;
          }
      }
  </code></pre>
  
  <p>this doesn't fire because my datagrid column is not bound to the SelectedProjectExpenseItem.ItemNumber, it is bound to the ItemNumber in the ObservableCollection.</p>
  
  <p>I am looking for any guidance as this is really confusing me.</p>
  
  <p><B>---------------------------- EDIT: ----------------------------</B></p>
  
  <p>Ok, I created a separate viewmodel for my DTO:</p>
  
  <pre><code>namespace ProjectExpense.ViewModels
  {
      public class ProjectExpenseItemsDtoViewModel : ProjectExpenseItemsDto, IDataErrorInfo
      {
          public ProjectExpenseItemsDtoViewModel()
          {
              Initialize();
          }
  
          private void Initialize()
          {
          }
  
          #region Validation
  
          // string method
          static bool IsStringMissing(string value)
          {
              return String.IsNullOrEmpty(value) || value.Trim() == String.Empty;
          }
  
          #endregion
  
          #region IDataErrorInfo Members
  
          public string Error
          {
              get
              {
                  return this[string.Empty];
              }
          }
  
          public string this[string propertyName]
          {
              get
              {
                  string result = string.Empty;
                  if (propertyName == "ItemNumber")
                  {
                      if (IsStringMissing(this.ItemNumber))
                          result = "Item number cannot be empty!";
                      if (this.ItemNumber.Length &gt; 50)
                          return "Item number exceeds 50 characters";
                  }
                  return result;
              }
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>Now, I am having problems with the following line in my main vm:</p>
  
  <pre><code>IList&lt;ProjectExpenseItemsDtoViewModel&gt; iList = projectExpenseItemsRepository.GetProjectExpenseItems(ProjectExpenseID);
  foreach (ProjectExpenseItemsDtoViewModel item in iList)
     ListOfProjectExpenseItems.Add(item);
  </code></pre>
  
  <p>It says:</p>
  
  <p>Cannot implicitly convert type 'System.Collections.Generic.IList' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)</p>
  
  <p>Any ideas?</p>
  
  <p><B>---------------------------- EDIT: ----------------------------</B></p>
  
  <p>I found this link, gonna see if I can copy what the person is doing:</p>
  
  <p><a href="http://blogs.u2u.be/diederik/post/2009/10/30/Validation-in-a-WPF-DataGrid.aspx" rel="nofollow noreferrer">Validation-in-a-WPF-DataGrid</a></p>