예제 #1
0
        private void LoadStudents()
        {
            Task.Run(() => {
                try
                {
                    var students = _model.GetStudents();
                    if (students == null)
                    {
                        return;
                    }

                    List <StudentWrapper> wrappers = new List <StudentWrapper>();
                    foreach (var item in students)
                    {
                        StudentWrapper wrapper = new StudentWrapper();
                        wrapper.Student        = item;
                        wrapper.RemoveCmd      = new BaseCommand(() => { RemoveCmdExecute(wrapper); });
                        wrapper.EditCmd        = new BaseCommand(() => { EditCmdExecute(wrapper); });
                        wrappers.Add(CreateStudentWraper(item));
                    }

                    App.Current.Dispatcher.Invoke(() => {
                        Students = new ObservableCollection <StudentWrapper>(wrappers);
                        Students.CollectionChanged += Students_CollectionChanged;
                    });
                }
                catch { }
            });
        }
예제 #2
0
        private StudentWrapper CreateStudentWraper(Student student)
        {
            StudentWrapper wrapper = new StudentWrapper();

            wrapper.Student   = student;
            wrapper.RemoveCmd = new BaseCommand(() => { RemoveCmdExecute(wrapper); });
            wrapper.EditCmd   = new BaseCommand(() => { EditCmdExecute(wrapper); });
            return(wrapper);
        }
예제 #3
0
        private void EditCmdExecute(StudentWrapper item)
        {
            Student _edit = new Student();

            _edit.Id        = item.Student.Id;
            _edit.Age       = item.Student.Age;
            _edit.FirstName = item.Student.FirstName;
            _edit.Gender    = item.Student.Gender;
            _edit.Last      = item.Student.Last;
            App.Current.Dispatcher.Invoke(() => {
                EditableStudent = _edit;
            });
        }
예제 #4
0
 private void RemoveCmdExecute(StudentWrapper item)
 {
     try
     {
         if (MessageBox.Show("Вы точно хотите удалить пользователя?", "Сообщение", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
         {
             var stds = GetStudentsFromWrappers().Where(i => i != item.Student);
             _model.SaveChanges(stds);
             App.Current.Dispatcher.Invoke(() =>
             {
                 Students.Remove(item);
                 if (EditableStudent != null && EditableStudent.Id == item.Student.Id)
                 {
                     EditableStudent = null;
                 }
             });
         }
     }
     catch { }
 }