Limitation in Implementing IDataErrorInfo or INotifyDataErrorInfo in WPF application

678 Views Asked by At

I'm developing a WPF application (MVVM). I have a class from a separate assembly (Odata V4 Generated Proxy Class).

public partial class Book : BaseEntityType, INotifyPropertyChanged
{
   public string Title{get;set;}
   ...
}

Now I need to decorate the properties of that class with data annotations, in order to validate the properties using either IDataErrorInfo or INotifyDataErrorInfo interface. Ex:

[Required]
public string Title{get;set;}

Problem:

As this class is in separate assembly, I cannot create a partial class. Properties of this class are bounded to UI elements of application. Now I need to validate the properties, when user input's data.

I'm struggling to design the solution. Could any one help me to acheive this?

1

There are 1 best solutions below

0
On

A wrapper might look like this:

public class BookWrapper : INotifyPropertyChanged, IDataErrorInfo
{
   private Book _book;
     public Book Book 
   {
      get
       {
         return _book;
       }
      set
       {
         _book-value;
         NotifyPropertyChanged("Book");
       }
   }  
    public string Error
    {
        get { return String.Empty; }
    }
    public string this[string columnName]
    {
        get
        {
            String errorMessage = String.Empty;
            switch (columnName)
            {
                case "Book":
                    if (Book.IsValid==false)
                    {
                        errorMessage = "Book not valid";
                    }
                    break;
            }
            return errorMessage;
        }
    }
  INotifyPropertyChanged Implementation...
}

This way you don't mess with your data object(Book) and you dont litter it with unnecessary interfaces.