Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Building WPF Applications with Self-Tracking Entity Generator - Project Setup

, 20 Feb 2012
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator for WPF/Silverlight.
SchoolSample_20111201.zip
SchoolSample
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
Assemblies
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
ComponentModel.Composition.Initialization.Desktop
Microsoft
ComponentModel
Composition
Hosting
Internal
Properties
System
ComponentModel
Composition
Hosting
System.Windows.Controls.Data.Input
Common
Properties
Settings.settings
themes
Validation
Database
SchoolSample
SchoolSample.csproj.user
Asset
Control
Properties
Settings.settings
View
SchoolSample.Common
Model
Properties
Resource
SchoolSample.Data
EntityModel
Properties
Validation
SchoolSample.Data.Wcf
EntityModel
SchoolModel.edmx
Properties
Validation
SchoolSample.Model
Properties
SchoolSample.ViewModel
Properties
SchoolSample.Wcf
SchoolSample.Wcf.csproj.user
Properties
Service
SchoolService.svc
SchoolSample.WCFService
SchoolSample.WCFService.csproj.user
Properties
Service References
SchoolService
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
Test.SchoolSample.Model
Properties
Test References
SchoolSample.Model.accessor
Test.SchoolSample.ViewModel
Properties
Test References
SchoolSample.ViewModel.accessor
SchoolSample_20120117.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
SchoolSample_20120216.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using SchoolSample.Common;
using SchoolSample.EntityModel;

namespace SchoolSample.ViewModel
{
    [Export(ViewModelTypes.StudentPageViewModel, typeof (ViewModelBase))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class StudentPageViewModel : ViewModelBase
    {
        #region "Private Data Members"

        private ISchoolModel _schoolModel;

        #endregion "Private Data Members"

        #region "Constructor"

        [ImportingConstructor]
        public StudentPageViewModel(ISchoolModel schoolModel)
        {
            _schoolModel = schoolModel;

            // set up event handling
            _schoolModel.PropertyChanged += _schoolModel_PropertyChanged;
            _schoolModel.GetStudentsCompleted += _schoolModel_GetStudentsCompleted;
            _schoolModel.GetStudentByIdCompleted += _schoolModel_GetStudentByIdCompleted;
            _schoolModel.SaveStudentChangesCompleted += _schoolModel_SaveStudentChangesCompleted;

            PropertyChanged += StudentPageViewModel_PropertyChanged;

            // get students
            _schoolModel.GetStudentsAsync("Enrollments.Course", "StudentPage");

            // set up initial screen status
            StudentFormEndEdit();
        }

        #endregion "Constructor"

        #region "Public Properties"

        /// <summary>
        /// All students collection returns/updates StudentsList from _schoolModel,
        /// _allStudentsCache keeps a local reference to the list and it syncs with
        /// _schoolModel.StudentsList when navigates to this page
        /// </summary>
        private ObservableCollection<Student> AllStudents
        {
            get { return _schoolModel != null ? _schoolModel.StudentsList : null; }
            set
            {
                if (!ReferenceEquals(_allStudentsCache, value))
                {
                    _allStudentsCache = value;
                    // update StudentsList on the model
                    if (_schoolModel != null) _schoolModel.StudentsList = _allStudentsCache;
                }
            }
        }

        private ObservableCollection<Student> _allStudentsCache;

        public CollectionViewSource AllStudentsSource
        {
            get { return _allStudentsSource; }
            private set
            {
                if (!ReferenceEquals(_allStudentsSource, value))
                {
                    _allStudentsSource = value;
                    RaisePropertyChanged("AllStudentsSource");
                }
            }
        }

        private CollectionViewSource _allStudentsSource;

        /// <summary>
        /// CurrentStudent returns/updates CurrentStudent from _schoolModel,
        /// _currentStudentCache keeps a local reference to the object and it
        /// syncs with _schoolModel.CurrentStudent when navigates to this page 
        /// </summary>
        public Student CurrentStudent
        {
            get { return _schoolModel != null ? _schoolModel.CurrentStudent : null; }
            private set
            {
                if (!ReferenceEquals(_currentStudentCache, value))
                {
                    _currentStudentCache = value;
                    if (_schoolModel != null) _schoolModel.CurrentStudent = _currentStudentCache;
                    RaisePropertyChanged("CurrentStudent");
                }
            }
        }

        private Student _currentStudentCache;

        public bool StudentFormInEdit
        {
            get { return _studentFormInEdit; }
            private set
            {
                if (_studentFormInEdit != value)
                {
                    _studentFormInEdit = value;
                    RaisePropertyChanged("StudentFormInEdit");
                }
            }
        }

        private bool _studentFormInEdit;

        public bool CurrentStudentHasErrors
        {
            get { return _currentStudentHasErrors; }
            set
            {
                if (_currentStudentHasErrors != value)
                {
                    _currentStudentHasErrors = value;
                    RaisePropertyChanged("CurrentStudentHasErrors");
                }
            }
        }

        private bool _currentStudentHasErrors;

        public bool StudentListIsEnabled
        {
            get { return _studentListIsEnabled; }
            private set
            {
                if (_studentListIsEnabled != value)
                {
                    _studentListIsEnabled = value;
                    RaisePropertyChanged("StudentListIsEnabled");
                }
            }
        }

        private bool _studentListIsEnabled = true;

        #endregion "Public Properties"

        #region "Public Commands"

        /// <summary>
        /// Command for Page Loaded event
        /// </summary>
        public RelayCommand PageLoadedCommand
        {
            get
            {
                if (_pageLoadedCommand == null)
                {
                    _pageLoadedCommand = new RelayCommand(
                        OnPageLoadedCommand);
                }
                return _pageLoadedCommand;
            }
        }

        private RelayCommand _pageLoadedCommand;

        private void OnPageLoadedCommand()
        {
            // synchronize _schoolModel.StudentsList with local cache
            if (_schoolModel != null && _allStudentsCache != null)
                _schoolModel.StudentsList = _allStudentsCache;
            // synchronize _schoolModel.CurrentStudent with local cache
            if (_schoolModel != null && _currentStudentCache != null)
                _schoolModel.CurrentStudent = _currentStudentCache;
        }

        /// <summary>
        /// Command for Page Unloaded event
        /// </summary>
        public RelayCommand PageUnLoadedCommand
        {
            get
            {
                if (_pageUnLoadedCommand == null)
                {
                    _pageUnLoadedCommand = new RelayCommand(
                        OnPageUnLoadedCommand);
                }
                return _pageUnLoadedCommand;
            }
        }

        private RelayCommand _pageUnLoadedCommand;

        private void OnPageUnLoadedCommand()
        {
            // clean up
            if (_schoolModel != null)
            {
                _schoolModel.StudentsList = null;
                _schoolModel.CurrentStudent = null;
            }
        }

        /// <summary>
        /// Add student command. We can add a new
        /// student when student form is not in edit
        /// </summary>
        public RelayCommand AddStudentCommand
        {
            get
            {
                if (_addStudentCommand == null)
                {
                    _addStudentCommand = new RelayCommand(
                        OnAddStudentCommand,
                        () => !StudentFormInEdit);
                }
                return _addStudentCommand;
            }
        }

        private RelayCommand _addStudentCommand;

        private void OnAddStudentCommand()
        {
            // create a temporary companyId
            int newPersonId = AllStudents.Count > 0
                                  ? ((from student in AllStudents select Math.Abs(student.PersonId)).Max() + 1)*(-1)
                                  : -1;
            // create a new student
            CurrentStudent = new Student
                                 {
                                     SuspendValidation = true,
                                     PersonId = newPersonId,
                                     Name = string.Empty,
                                     EnrollmentDate = DateTime.Now
                                 };
            CurrentStudent.SuspendValidation = false;
            // and begin edit
            OnEditCommitStudentCommand();
        }

        /// <summary>
        /// Delete student command. We can delete a student
        /// when the current student is not null, and student
        ///  form is not in edit
        /// </summary>
        public RelayCommand DeleteStudentCommand
        {
            get
            {
                if (_deleteStudentCommand == null)
                {
                    _deleteStudentCommand = new RelayCommand(
                        OnDeleteStudentCommand,
                        () => (CurrentStudent != null) && !StudentFormInEdit);
                }
                return _deleteStudentCommand;
            }
        }

        private RelayCommand _deleteStudentCommand;

        private void OnDeleteStudentCommand()
        {
            try
            {
                if (CurrentStudent != null && CurrentStudent.Enrollments.Count == 0)
                {
                    // ask to confirm deleting the current student
                    DialogMessage dialogMessage = new DialogMessage(
                        this,
                        ApplicationStrings.DeleteCurrentStudentMessageBoxText,
                        s =>
                            {
                                if (s == MessageBoxResult.OK)
                                {
                                    if (CurrentStudent.ChangeTracker.State == ObjectState.Added)
                                    {
                                        // if State is Added, simply remove from the StudentsList
                                        // as the database does not have this record yet
                                        AllStudents.Remove(CurrentStudent);
                                    }
                                    else
                                    {
                                        // if confirmed, remove current student
                                        CurrentStudent.MarkAsDeleted();
                                        _schoolModel.SaveStudentChangesAsync(false);
                                    }
                                }
                            })
                                                      {
                                                          Button = MessageBoxButton.OKCancel,
                                                          Caption = ApplicationStrings.ConfirmMessageBoxCaption
                                                      };
                    AppMessages.PleaseConfirmMessage.Send(dialogMessage);
                }
                else
                {
                    DialogMessage dialogMessage = new DialogMessage(
                        this,
                        ErrorResources.CurrentStudentEnrollmentNotEmptyMessageBoxText,
                        null)
                                                      {
                                                          Button = MessageBoxButton.OK,
                                                          Caption = ApplicationStrings.WarningMessageBoxCaption
                                                      };

                    AppMessages.StatusUpdateMessage.Send(dialogMessage);
                }
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }


        /// <summary>
        /// Edit/Commit a student. We can edit/commit a student
        /// when current student is not null
        /// </summary>
        public RelayCommand EditCommitStudentCommand
        {
            get
            {
                if (_editCommitStudentCommand == null)
                {
                    _editCommitStudentCommand = new RelayCommand(
                        OnEditCommitStudentCommand,
                        () => (CurrentStudent != null));
                }
                return _editCommitStudentCommand;
            }
        }

        private RelayCommand _editCommitStudentCommand;

        private void OnEditCommitStudentCommand()
        {
            if (CurrentStudent != null)
            {
                if (StudentFormInEdit)
                {
                    // if passed validation, end editing
                    if (!CurrentStudentHasErrors && CurrentStudent.TryValidate())
                    {
                        StudentFormEndEdit();
                        // if this is a new student, add to the StudentsList
                        if (CurrentStudent.ChangeTracker.State == ObjectState.Added)
                        {
                            var alreadyAdded = AllStudents.Any(n => n.PersonId == CurrentStudent.PersonId);
                            if (!alreadyAdded)
                            {
                                AllStudents.Add(CurrentStudent);
                                AllStudentsSource.View.MoveCurrentTo(CurrentStudent);
                            }
                        }
                    }
                }
                else
                {
                    StudentFormBeginEdit();
                }
            }
        }

        /// <summary>
        /// Cancel edit a student. We can cancel edit a student
        /// when current student is not null, and the student form
        /// is currently in edit
        /// </summary>
        public RelayCommand CancelEditStudentCommand
        {
            get
            {
                if (_cancelEditStudentCommand == null)
                {
                    _cancelEditStudentCommand = new RelayCommand(
                        OnCancelEditStudentCommand,
                        () => (CurrentStudent != null) && StudentFormInEdit);
                }
                return _cancelEditStudentCommand;
            }
        }

        private RelayCommand _cancelEditStudentCommand;

        private void OnCancelEditStudentCommand()
        {
            if (CurrentStudent != null)
            {
                StudentFormCancelEdit();
                // if this is a new student, simply discard that record
                if (CurrentStudent.ChangeTracker.State == ObjectState.Added)
                {
                    var alreadyAdded = AllStudents.Any(n => n.PersonId == CurrentStudent.PersonId);
                    if (!alreadyAdded)
                    {
                        CurrentStudent = null;
                        AllStudentsSource.View.MoveCurrentToFirst();
                        if (AllStudentsSource.View.CurrentItem != null)
                            CurrentStudent = (Student) AllStudentsSource.View.CurrentItem;
                    }
                }
            }
        }

        /// <summary>
        /// Refresh current student command. We can refresh
        /// current student when student form is not in edit, 
        /// and there is no change to save for the current 
        ///  student
        /// </summary>
        public RelayCommand RefreshStudentCommand
        {
            get
            {
                if (_refreshStudentCommand == null)
                {
                    _refreshStudentCommand = new RelayCommand(
                        OnRefreshStudentCommand,
                        () => !StudentFormInEdit && (_schoolModel != null)
                              && !(_schoolModel.CurrentStudentHasChanges));
                }
                return _refreshStudentCommand;
            }
        }

        private RelayCommand _refreshStudentCommand;

        private void OnRefreshStudentCommand()
        {
            if (CurrentStudent != null)
            {
                _schoolModel.GetStudentByIdAsync(CurrentStudent.PersonId);
            }
        }

        /// <summary>
        /// Submit change command. We can submit changes
        /// when student form is not in edit, and there 
        /// are changes to save for current student
        /// </summary>
        public RelayCommand SubmitStudentChangeCommand
        {
            get
            {
                if (_submitStudentChangeCommand == null)
                {
                    _submitStudentChangeCommand = new RelayCommand(
                        OnSubmitStudentChangeCommand,
                        () => !StudentFormInEdit && (_schoolModel != null)
                              && (_schoolModel.CurrentStudentHasChanges));
                }
                return _submitStudentChangeCommand;
            }
        }

        private RelayCommand _submitStudentChangeCommand;

        private void OnSubmitStudentChangeCommand()
        {
            try
            {
                if (!_schoolModel.IsBusy && CurrentStudent != null)
                {
                    // we only save changes when current student passed validation
                    var passedValidation = CurrentStudent.TryValidateObjectGraph();
                    if (!passedValidation) return;

                    // save changes for CurrentStudent only 
                    _schoolModel.SaveStudentChangesAsync(false);
                }
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        /// <summary>
        /// Cancel change command. We can cancel changes
        /// when student form is not in edit, and there 
        /// are changes to cancel for current student
        /// </summary>
        public RelayCommand CancelStudentChangeCommand
        {
            get
            {
                if (_cancelStudentChangeCommand == null)
                {
                    _cancelStudentChangeCommand = new RelayCommand(
                        OnCancelStudentChangeCommand,
                        () => !StudentFormInEdit && (_schoolModel != null)
                              && (_schoolModel.CurrentStudentHasChanges));
                }
                return _cancelStudentChangeCommand;
            }
        }

        private RelayCommand _cancelStudentChangeCommand;

        private void OnCancelStudentChangeCommand()
        {
            try
            {
                if (!_schoolModel.IsBusy && CurrentStudent != null)
                {
                    // ask to confirm canceling changes
                    DialogMessage dialogMessage = new DialogMessage(
                        this,
                        ApplicationStrings.CancelAnyChangesMessageBoxText,
                        s =>
                            {
                                if (s == MessageBoxResult.OK)
                                {
                                    // if confirmed, cancel changes for CurrentStudent
                                    _schoolModel.RejectStudentChanges(false);
                                }
                            })
                                                      {
                                                          Button = MessageBoxButton.OKCancel,
                                                          Caption = ApplicationStrings.ConfirmMessageBoxCaption
                                                      };

                    AppMessages.PleaseConfirmMessage.Send(dialogMessage);
                }
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        /// <summary>
        /// Refresh students list command. We can
        /// refresh students list when student form is
        /// not in edit, and there is no change to save
        /// </summary>
        public RelayCommand RefreshAllCommand
        {
            get
            {
                if (_refreshAllCommand == null)
                {
                    _refreshAllCommand = new RelayCommand(
                        OnRefreshAllCommand,
                        () => !StudentFormInEdit && (_schoolModel != null)
                              && !(_schoolModel.StudentsListHasChanges));
                }
                return _refreshAllCommand;
            }
        }

        private RelayCommand _refreshAllCommand;

        private void OnRefreshAllCommand()
        {
            if (!_schoolModel.IsBusy)
            {
                // refresh students
                _schoolModel.GetStudentsAsync("Enrollments.Course", "StudentPage");
            }
        }

        /// <summary>
        /// Submit change command. We can submit changes
        /// when student form is not in edit, and there are changes to save
        /// </summary>
        public RelayCommand SubmitAllChangeCommand
        {
            get
            {
                if (_submitAllChangeCommand == null)
                {
                    _submitAllChangeCommand = new RelayCommand(
                        OnSubmitAllChangeCommand,
                        () => !StudentFormInEdit && (_schoolModel != null)
                              && (_schoolModel.StudentsListHasChanges));
                }
                return _submitAllChangeCommand;
            }
        }

        private RelayCommand _submitAllChangeCommand;

        private void OnSubmitAllChangeCommand()
        {
            try
            {
                if (!_schoolModel.IsBusy)
                {
                    if (AllStudents != null)
                    {
                        // we only save changes when all students passed validation
                        var passedValidation = AllStudents.All(o => o.TryValidateObjectGraph());
                        if (!passedValidation) return;

                        _schoolModel.SaveStudentChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        /// <summary>
        /// Cancel change command. We can cancel changes
        /// when student form is not in edit, and there are changes to cancel
        /// </summary>
        public RelayCommand CancelAllChangeCommand
        {
            get
            {
                if (_cancelAllChangeCommand == null)
                {
                    _cancelAllChangeCommand = new RelayCommand(
                        OnCancelAllChangeCommand,
                        () => !StudentFormInEdit && (_schoolModel != null)
                              && (_schoolModel.StudentsListHasChanges));
                }
                return _cancelAllChangeCommand;
            }
        }

        private RelayCommand _cancelAllChangeCommand;

        private void OnCancelAllChangeCommand()
        {
            try
            {
                if (!_schoolModel.IsBusy)
                {
                    // ask to confirm canceling changes
                    DialogMessage dialogMessage = new DialogMessage(
                        this,
                        ApplicationStrings.CancelAnyChangesMessageBoxText,
                        s =>
                            {
                                if (s == MessageBoxResult.OK)
                                {
                                    // if confirmed, cancel changes for StudentsList
                                    _schoolModel.RejectStudentChanges();
                                }
                            })
                                                      {
                                                          Button = MessageBoxButton.OKCancel,
                                                          Caption = ApplicationStrings.ConfirmMessageBoxCaption
                                                      };

                    AppMessages.PleaseConfirmMessage.Send(dialogMessage);
                }
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        #endregion "Public Commands"

        #region "Private Methods"

        private void StudentFormBeginEdit()
        {
            if (CurrentStudent != null)
            {
                CurrentStudent.BeginEdit();
                AppMessages.BeginEditMessage.Send("Student");
                StudentFormInEdit = true;
            }
        }

        private void StudentFormEndEdit()
        {
            AppMessages.EndEditMessage.Send("Student");
            StudentFormInEdit = false;
            if (CurrentStudent != null) CurrentStudent.EndEdit();
        }

        private void StudentFormCancelEdit()
        {
            if (CurrentStudent != null)
            {
                AppMessages.CancelEditMessage.Send("Student");
                StudentFormInEdit = false;
                CurrentStudent.CancelEdit();
            }
        }

        /// <summary>
        /// Event handler for GetStudentsCompleted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _schoolModel_GetStudentsCompleted(object sender, ResultsArgs<Student> e)
        {
            var screenName = e.UserState as string;
            if (!string.Equals(screenName, "StudentPage")) return;

            if (!e.HasError)
            {
                // clear any previous error after a successful call
                _schoolModel.ClearLastError();

                // cancel any changes before setting AllStudentsSource
                if (_schoolModel.StudentsListHasChanges)
                {
                    _schoolModel.RejectStudentChanges();
                }

                AllStudents = new ObservableCollection<Student>(e.Results);

                AllStudentsSource = new CollectionViewSource {Source = AllStudents};
                // sort by student Id
                AllStudentsSource.SortDescriptions.Add(new SortDescription("PersonId", ListSortDirection.Ascending));
                AllStudentsSource.View.CurrentChanged += AllStudentsSourceView_CurrentChanged;

                if (AllStudents.Count >= 1)
                {
                    if (CurrentStudent != null)
                    {
                        var currentStudent = AllStudents
                            .FirstOrDefault(n => n.PersonId == CurrentStudent.PersonId);
                        if (currentStudent != null)
                            AllStudentsSource.View.MoveCurrentTo(currentStudent);
                        else
                            // set the first row as the current student in edit
                            AllStudentsSource.View.MoveCurrentToFirst();
                    }
                    else
                    {
                        // set the first row as the current student in edit
                        AllStudentsSource.View.MoveCurrentToFirst();
                    }
                    CurrentStudent = (Student) AllStudentsSource.View.CurrentItem;
                }
                else
                {
                    CurrentStudent = null;
                }
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }

        /// <summary>
        /// Event handler for CurrentChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AllStudentsSourceView_CurrentChanged(object sender, EventArgs e)
        {
            CurrentStudent = (Student) AllStudentsSource.View.CurrentItem;
        }

        /// <summary>
        /// Event handler for GetStudentByIdCompleted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _schoolModel_GetStudentByIdCompleted(object sender, ResultArgs<Student> e)
        {
            if (!e.HasError)
            {
                // clear any previous error after a successful call
                _schoolModel.ClearLastError();

                Student currentStudent = e.Results;
                if (currentStudent != null)
                {
                    // find the matching current student from AllStudents list
                    Student matchedStudent = AllStudents.Single(n => n.PersonId == currentStudent.PersonId);
                    // replace matchedStudent from AllStudents list with what is returned from GetStudentById
                    int index = AllStudents.IndexOf(matchedStudent);
                    AllStudents.Remove(matchedStudent);
                    AllStudents.Insert(index, currentStudent);
                    AllStudentsSource.View.MoveCurrentTo(currentStudent);
                }
                else
                {
                    DialogMessage dialogMessage = new DialogMessage(
                        this,
                        ErrorResources.CurrentStudentDoesNotExistMessageBoxText,
                        null)
                                                      {
                                                          Button = MessageBoxButton.OK,
                                                          Caption = ApplicationStrings.WarningMessageBoxCaption
                                                      };

                    AppMessages.StatusUpdateMessage.Send(dialogMessage);
                }
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }

        /// <summary>
        /// Event handler for SaveStudentChangesCompleted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _schoolModel_SaveStudentChangesCompleted(object sender, ResultArgs<string> e)
        {
            if (!e.HasError)
            {
                // clear any previous error after a successful call
                _schoolModel.ClearLastError();

                // check whether there is any warning message returned
                if (!string.IsNullOrEmpty(e.Results))
                {
                    DialogMessage dialogMessage = new DialogMessage(
                        this,
                        e.Results,
                        null)
                                                      {
                                                          Button = MessageBoxButton.OK,
                                                          Caption = ApplicationStrings.WarningMessageBoxCaption
                                                      };

                    AppMessages.StatusUpdateMessage.Send(dialogMessage);

                    // If last operation is a delete operation, call reject changes
                    if (CurrentStudent != null && CurrentStudent.ChangeTracker.State == ObjectState.Deleted)
                    {
                        _schoolModel.RejectStudentChanges(false);
                    }
                }
                else
                {
                    if (_schoolModel.StudentsListHasChanges)
                    {
                        if (!_schoolModel.CurrentStudentHasChanges)
                            // only refresh current student after Insert/Update/Delete
                            OnRefreshStudentCommand();
                    }
                    else
                        // refresh the StudentsList after Insert/Update/Delete
                        OnRefreshAllCommand();
                }
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }

        /// <summary>
        /// Event handler for PropertyChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _schoolModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "StudentsListHasChanges":
                    RefreshAllCommand.RaiseCanExecuteChanged();
                    SubmitAllChangeCommand.RaiseCanExecuteChanged();
                    CancelAllChangeCommand.RaiseCanExecuteChanged();
                    break;
                case "CurrentStudentHasChanges":
                    RefreshStudentCommand.RaiseCanExecuteChanged();
                    SubmitStudentChangeCommand.RaiseCanExecuteChanged();
                    CancelStudentChangeCommand.RaiseCanExecuteChanged();
                    break;
            }
        }

        /// <summary>
        /// Event handler for PropertyChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StudentPageViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "CurrentStudent":
                    DeleteStudentCommand.RaiseCanExecuteChanged();
                    EditCommitStudentCommand.RaiseCanExecuteChanged();
                    CancelEditStudentCommand.RaiseCanExecuteChanged();
                    break;
                case "StudentFormInEdit":
                    StudentListIsEnabled = !StudentFormInEdit;
                    AddStudentCommand.RaiseCanExecuteChanged();
                    DeleteStudentCommand.RaiseCanExecuteChanged();
                    CancelEditStudentCommand.RaiseCanExecuteChanged();
                    RefreshStudentCommand.RaiseCanExecuteChanged();
                    SubmitStudentChangeCommand.RaiseCanExecuteChanged();
                    CancelStudentChangeCommand.RaiseCanExecuteChanged();
                    RefreshAllCommand.RaiseCanExecuteChanged();
                    SubmitAllChangeCommand.RaiseCanExecuteChanged();
                    CancelAllChangeCommand.RaiseCanExecuteChanged();
                    break;
            }
        }

        #endregion "Private Methods"
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Weidong Shen
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 20 Feb 2012
Article Copyright 2011 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid