I am using MVVM-Light and for every view model I have to create an implementation of the INotifyDataErrorInfo which in some cases uses same methods to validate same property types. In this example I am using DateTime:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using SL.Resources;
namespace SL.ViewModel
{
public partial class AdministrationViewModel : INotifyDataErrorInfo
{
#region Validations
public void isDateToValid(DateTime? value, DateTime? dateFrom, string propertyName)
{
//check if null
if (value == null) AddError(propertyName, CommonErrors.DateNull_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNull_ERROR);
//check if min and max value
if (value < DateTime.MinValue || value > DateTime.MaxValue) AddError(propertyName, CommonErrors.DateNotValid_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNotValid_ERROR);
if (value < dateFrom) AddError(propertyName, CommonErrors.DateFromSmall_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateFromSmall_ERROR);
}
public void IsDateValid(DateTime? value, string propertyName)
{
if (value == null) AddError(propertyName, CommonErrors.DateNull_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNull_ERROR);
if (value < DateTime.MinValue || value > DateTime.MaxValue) AddError(propertyName, CommonErrors.DateNotValid_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNotValid_ERROR);
}
#endregion
#region INotifyDataErrorInfo Members
public Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();
// Adds the specified error to the errors collection if it is not
// already present, inserting it in the first position if isWarning is
// false. Raises the ErrorsChanged event if the collection changes.
public void AddError(string propertyName, string error, bool isWarning)
{
if (!errors.ContainsKey(propertyName))
errors[propertyName] = new List<string>();
if (!errors[propertyName].Contains(error))
{
if (isWarning) errors[propertyName].Add(error);
else errors[propertyName].Insert(0, error);
RaiseErrorsChanged(propertyName);
}
}
// Removes the specified error from the errors collection if it is
// present. Raises the ErrorsChanged event if the collection changes.
public void RemoveError(string propertyName, string error)
{
if (errors.ContainsKey(propertyName) &&
errors[propertyName].Contains(error))
{
errors[propertyName].Remove(error);
if (errors[propertyName].Count == 0) errors.Remove(propertyName);
RaiseErrorsChanged(propertyName);
}
}
public void RaiseErrorsChanged(string propertyName)
{
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
public event System.EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName) ||
!errors.ContainsKey(propertyName)) return null;
return errors[propertyName];
}
public bool HasErrors
{
get { return errors.Count > 0; }
}
#endregion
}
}
How could I make this code reusable in other view models, so I don't have to implement the same thing over and over again?
I created a class that implements INotifyDataErrorInfo :
public class ViewModelValidation : INotifyDataErrorInfo
But when I want to use it in my view model it doesn't work:
public partial class AdministrationViewModel : ViewModelValidation
Error:
Partial declarations of 'SL.ViewModel.AdministrationViewModel' must not specify different base classes...
this is because in my main view model file I have a base class from MVVM-Light:
public partial class AdministrationViewModel : ViewModelBase
Any help resolving this is appretiated.
I figured it out myself. I created a ViewModelCommon
class based on ViewModelBase
from MVVM-Light and added the INotifyDataErrorInfo interface to it:
public class ViewModelCommon : ViewModelBase, INotifyDataErrorInfo
Then in my view model code instead of ViewModelBase
I just used my ViewModelCommon
class:
public partial class AdministrationViewModel : ViewModelCommon
And it works just fine.