示例#1
0
        public MedRecordViewModel(Employee employee)
        {
            DownloadMedRecords();
            currentemployee = employee;

            FilterCollectionView.Filter = i =>
            {
                if (_searchKey == "" || _searchKey == null)
                {
                    return(true);
                }
                if (string.IsNullOrEmpty(FilterString))
                {
                    return(true);
                }
                if (_searchKey == "Фамилия")
                {
                    MedRecord m = i as MedRecord;
                    return(m.Surname.ToString().StartsWith(FilterString));
                }
                if (_searchKey == "Имя")
                {
                    MedRecord m = i as MedRecord;
                    return(m.Name.ToString().StartsWith(FilterString));
                }
                if (_searchKey == "Отчество")
                {
                    MedRecord m = i as MedRecord;
                    return(m.Patronymic.ToString().StartsWith(FilterString));
                }
                if (_searchKey == "Город")
                {
                    MedRecord m = i as MedRecord;
                    return(m.City.ToString().StartsWith(FilterString));
                }
                if (_searchKey == "Улица")
                {
                    MedRecord m = i as MedRecord;
                    return(m.Street.ToString().StartsWith(FilterString));
                }
                if (_searchKey == "Дом")
                {
                    MedRecord m = i as MedRecord;
                    return(m.Home.ToString().StartsWith(FilterString));
                }
                else
                {
                    return(true);
                }
            };

            EditMedRecord = new DelegateCommand(() =>
            {
                editoradd = false;
                selectedMedRecord?.BeginEdit();

                var w         = new MedRecordEdit();
                w.DataContext = this;
                w.ShowDialog();
            }, () => selectedMedRecord != null);


            AddMedRecord = new DelegateCommand(() =>
            {
                editoradd = true;

                MedRecord NewMedRecord = new MedRecord();
                SelectedMedRecord      = NewMedRecord;

                var w         = new MedRecordEdit();
                w.DataContext = this;
                w.ShowDialog();
            });

            SaveMedRecord = new DelegateCommand(() =>
            {
                if (editoradd == true)
                {
                    context.MedRecord.Add(selectedMedRecord);

                    context.SaveChanges();

                    MedRecords.Add(SelectedMedRecord);
                }
                else
                {
                    context.Entry(selectedMedRecord).State = EntityState.Modified;
                    context.SaveChanges();
                }
            });

            RemoveMedRecord = new DelegateCommand(() =>
            {
                if (MessageBox.Show("Удалить?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    context.Entry(selectedMedRecord).State = EntityState.Deleted;
                    MedRecords.Remove(selectedMedRecord);
                    context.SaveChanges();
                }
                else
                {
                    return;
                }
            }, () => selectedMedRecord != null);

            CanselEdit = new DelegateCommand(() =>
            {
                if (editoradd == false)
                {
                    if (selectedMedRecord == null)
                    {
                        return;
                    }
                    SelectedMedRecord.CancelEdit();
                }
                else
                {
                    return;
                }
            });

            MedRecordOpen = new DelegateCommand(() =>
            {
                MedRecordOpenViewModel viewmodel = new MedRecordOpenViewModel(SelectedMedRecord, currentemployee);
                var w         = new OpenMedRecord();
                w.DataContext = viewmodel;
                w.ShowDialog();
            }, () => currentemployee.Post.Name == "Врач" || currentemployee.Post.Name == "Администратор");
        }