I have a MyViewModel, which contains scalar properties and collection properties.
public class MyViewModel :
System.ComponentModel.INotifyPropertyChanged,
System.ComponentModel.IDataErrorInfo
{
public MyViewModel()
{
List<SelectableObject> list = new List<SelectableObject>();
foreach (var weekDay in System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames)
{
list.Add(new SelectableObject() { Name = weekDay, IsSelected = false });
}
WeekDays = list;
}
public string Catalog { get; set; }
public DateTime CreationTime { get; set; }
public DateTime ModificationTime { get; set; }
public IEnumerable<SelectableObject> WeekDays { get; private set; }
public ICommand SaveCommand { get; private set; }
public string Error
{
get { return string.Empty; }
}
public string this[string columnName]
{
get
{
var errorResult = string.Empty;
switch (columnName)
{
case "CreationTime":
// Validation logic
break;
case "ModificationTime":
// Validation logic
break;
default:
break;
}
return errorResult;
}
}
}
SelectableObject class:
public class SelectableObject : System.ComponentModel.INotifyPropertyChanged
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
I want to manipulate IsEnabled property of the <Button /> via SaveCommand, and I want it will enabled, only if there no errors on the form. But I can't figure out how to validate this: at least one WeekDay had to be selected.
Yes, I can listen for PropertyChanged event of every object in the WeekDays collection, but in this case validation logic will be separated from the IDataErrorInfo interface.
Is there any solution for this problem? How to put validation logic for collections to the IDataErrorInfo interface?