0

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?

stukselbax
  • 5,855
  • 3
  • 32
  • 54

1 Answers1

1

simply do your logic in your canexecute?

viewmodel:

private DelegateCommand _save;

public ICommand SaveCommand
{
  get {return this._save ?? (this._save = new DelegateComamnd(this.MyExecuteMethod, this.MyCanExecuteMethod));}
 }

private bool MyCanExecuteMethod()
{
   return this.WeekDays.Any(x=>x.IsSelected) && string.IsNullOrWhiteSpace(this.Error);
}

ps: error should be non empty if there any errors.

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Is this the only way to do it? In your case I have to raise CanSaveCommandExecute any time when IsSelected property of weekdays is changed - so I have to handle PropertyChanged event... – stukselbax Jul 30 '12 at 07:54
  • no no you dont have to raise anything - wpf do this for you. i dont know if you use RelayCommand or DelegateCommand but both have a CanExecute method and these method is called from CommandManager in wpf. you just have to provide this method. just try this out. – blindmeis Jul 30 '12 at 08:09
  • I use DelegateCommand from prism library. So every time property changed on the ViewModel I have to raise CanExecuteChanged. Its good way, because I'm control the moments when to raise the event. – stukselbax Jul 30 '12 at 08:21
  • i must admit that i have my own delegatecommand implementation with somthing like this in my CanExecute add/remove: CommandManager.RequerySuggested += value; i dont know where/how prism handle these behavior. do you get an automatic update to your command/Save Button if you try my solution and just update one weekday? – blindmeis Jul 30 '12 at 09:35
  • [Here](http://stackoverflow.com/a/7353704/780798) guy explain why prism DelegateCommand will not updated automatically. – stukselbax Jul 30 '12 at 10:28
  • What I want to say - in your case validation logic will be divided into several parts (one from indexer, other one - in CanExecute method) – stukselbax Jul 30 '12 at 10:50