Example #1
0
        public ContactsViewModel(IWorkUnitProvider workUnitProvider)
        {
            WorkUnitProvider = workUnitProvider;

            LoadAllContacts = new SimplerCommand
            {
                Action = () =>
                {
                    WorkUnit       = WorkUnitProvider.ProvideWorkUnit();
                    LoadedContacts = new ObservableCollection <Contact>(WorkUnit.Contacts.GetAll());
                    NotifyChangesMade();
                }
            };
            SaveAllContacts = new SimpleCommand
            {
                Action = () => {
                    WorkUnit.Save();
                    LoadedContacts.ToList().ForEach(c => WorkUnit.Reload(c));
                    NotifyChangesMade();
                },
                CanExecuteFunc = () => WorkUnit?.IsChanged() ?? false
            };
            OnEditEnding = new SimplerCommand
            {
                Action = SaveAllContacts.RaiseCanExecuteChanged
            };
            DeleteSelected = new SimpleCommand
            {
                Action = () =>
                {
                    SelectedContacts.ToList().ForEach(c => LoadedContacts.Remove(c));
                },
                CanExecuteFunc = () => {
                    return(SelectedContacts?.Count > 0);
                }
            };
            OnSelectionChange = new SimplerCommand <IList>
            {
                Action = o =>
                {
                    SelectedContacts = new ObservableCollection <Contact>(o.OfType <Contact>());
                }
            };
        }