Click here to Skip to main content
15,891,372 members
Articles / Desktop Programming / WPF

Building WPF Applications with Self-Tracking Entity Generator and Visual Studio 2012 - Project Setup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
17 Mar 2013CPOL8 min read 68.7K   3.5K   44  
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator and Visual Studio 2012.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using SchoolSample.Common;
using SchoolSample.EntityModel;
using SchoolSample.EntityModel.Resource;

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

        private NotificationMessageAction<ClientFilter<Student>> _notification;
        private int _currentMaxSortOrder;

        #endregion "Private Data Members"

        #region "Constructor"

        public StudentPageFilterViewModel()
        {
            // register for StudentFilterMessage
            AppMessages.StudentFilterMessage.Register(this, OnStudentFilterMessage);

            // set enum type
            ListSortDirectionCollection = new EnumCollection<ListSortDirection>(true,
                                                                                SchoolModelResource.ResourceManager);
            JointConditionCollection = new EnumCollection<JointConditionEnum>(false, SchoolModelResource.ResourceManager);
            StudentStatusCollection = new EnumCollection<StatusEnum>(false, SchoolModelResource.ResourceManager);

            CompareOperator = new List<string> {">=", ">", "=", "<", "<="};
            StatusCompareOperator = new List<string> {"=", "!="};

            ResetAll();
        }

        #endregion "Constructor"

        #region "Public Properties"

        public EnumCollection<ListSortDirection> ListSortDirectionCollection { get; private set; }
        public EnumCollection<JointConditionEnum> JointConditionCollection { get; private set; }
        public EnumCollection<StatusEnum> StudentStatusCollection { get; private set; }
        public List<string> CompareOperator { get; private set; }
        public List<string> StatusCompareOperator { get; private set; }

        public string StudentName
        {
            get { return _studentName; }
            set
            {
                if (!ReferenceEquals(_studentName, value))
                {
                    _studentName = value;
                    RaisePropertyChanged("StudentName");
                }
            }
        }

        private string _studentName;

        public int StudentNameSortDirectionIndex
        {
            get { return _studentNameSortDirectionIndex; }
            set
            {
                if (_studentNameSortDirectionIndex != value)
                {
                    _studentNameSortDirectionIndex = value;
                    RaisePropertyChanged("StudentNameSortDirectionIndex");
                }
            }
        }

        private int _studentNameSortDirectionIndex;

        public int? StudentNameSortOrder
        {
            get { return _studentNameSortOrder; }
            set
            {
                if (!ReferenceEquals(_studentNameSortOrder, value))
                {
                    _studentNameSortOrder = value;
                    RaisePropertyChanged("StudentNameSortOrder");
                }
            }
        }

        private int? _studentNameSortOrder;

        public DateTime? EnrollmentDate
        {
            get { return _enrollmentDate; }
            set
            {
                if (!ReferenceEquals(_enrollmentDate, value))
                {
                    _enrollmentDate = value;
                    RaisePropertyChanged("EnrollmentDate");
                }
            }
        }

        private DateTime? _enrollmentDate;

        public int EnrollmentDateCompareOperatorIndex
        {
            get { return _enrollmentDateCompareOperatorIndex; }
            set
            {
                if (_enrollmentDateCompareOperatorIndex != value)
                {
                    _enrollmentDateCompareOperatorIndex = value;
                    RaisePropertyChanged("EnrollmentDateCompareOperatorIndex");
                }
            }
        }

        private int _enrollmentDateCompareOperatorIndex;

        public int EnrollmentDateSortDirectionIndex
        {
            get { return _enrollmentDateSortDirectionIndex; }
            set
            {
                if (_enrollmentDateSortDirectionIndex != value)
                {
                    _enrollmentDateSortDirectionIndex = value;
                    RaisePropertyChanged("EnrollmentDateSortDirectionIndex");
                }
            }
        }

        private int _enrollmentDateSortDirectionIndex;

        public int? EnrollmentDateSortOrder
        {
            get { return _enrollmentDateSortOrder; }
            set
            {
                if (!ReferenceEquals(_enrollmentDateSortOrder, value))
                {
                    _enrollmentDateSortOrder = value;
                    RaisePropertyChanged("EnrollmentDateSortOrder");
                }
            }
        }

        private int? _enrollmentDateSortOrder;

        public StatusEnum? Status
        {
            get { return _status; }
            set
            {
                if (!ReferenceEquals(_status, value))
                {
                    _status = value;
                    RaisePropertyChanged("Status");
                }
            }
        }

        private StatusEnum? _status;

        public int StatusCompareOperatorIndex
        {
            get { return _statusCompareOperatorIndex; }
            set
            {
                if (_statusCompareOperatorIndex != value)
                {
                    _statusCompareOperatorIndex = value;
                    RaisePropertyChanged("StatusCompareOperatorIndex");
                }
            }
        }

        private int _statusCompareOperatorIndex;

        public int StatusSortDirectionIndex
        {
            get { return _statusSortDirectionIndex; }
            set
            {
                if (_statusSortDirectionIndex != value)
                {
                    _statusSortDirectionIndex = value;
                    RaisePropertyChanged("StatusSortDirectionIndex");
                }
            }
        }

        private int _statusSortDirectionIndex;

        public int? StatusSortOrder
        {
            get { return _statusSortOrder; }
            set
            {
                if (!ReferenceEquals(_statusSortOrder, value))
                {
                    _statusSortOrder = value;
                    RaisePropertyChanged("StatusSortOrder");
                }
            }
        }

        private int? _statusSortOrder;

        public int JointCondition1Index
        {
            get { return _jointCondition1Index; }
            set
            {
                if (_jointCondition1Index != value)
                {
                    _jointCondition1Index = value;
                    RaisePropertyChanged("JointCondition1Index");
                }
            }
        }

        private int _jointCondition1Index;

        public int JointCondition2Index
        {
            get { return _jointCondition2Index; }
            set
            {
                if (_jointCondition2Index != value)
                {
                    _jointCondition2Index = value;
                    RaisePropertyChanged("JointCondition2Index");
                }
            }
        }

        private int _jointCondition2Index;

        public bool? DialogResult
        {
            get { return _dialogResult; }
            set
            {
                if (!ReferenceEquals(_dialogResult, value))
                {
                    _dialogResult = value;
                    RaisePropertyChanged("DialogResult");
                }
            }
        }

        private bool? _dialogResult;

        #endregion "Public Properties"

        #region "Public Commands"

        /// <summary>
        /// Command for the OK button
        /// </summary>
        public RelayCommand OkCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    _okCommand = new RelayCommand(
                        OnOKCommand);
                }
                return _okCommand;
            }
        }

        private RelayCommand _okCommand;

        private void OnOKCommand()
        {
            if (_notification != null)
            {
                var clientFilter = ParseClientFilter();

                _notification.Execute(clientFilter);
                DialogResult = true;
            }
        }

        /// <summary>
        /// Command for the Cancel button
        /// </summary>
        public RelayCommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand(
                        OnCancelCommand);
                }
                return _cancelCommand;
            }
        }

        private RelayCommand _cancelCommand;

        private void OnCancelCommand()
        {
            DialogResult = false;
        }

        /// <summary>
        /// Command for the Save as Default button
        /// </summary>
        public RelayCommand SaveDefaultCommand
        {
            get
            {
                if (_saveDefaultCommand == null)
                {
                    _saveDefaultCommand = new RelayCommand(
                        OnSaveDefaultCommand);
                }
                return _saveDefaultCommand;
            }
        }

        private RelayCommand _saveDefaultCommand;

        private void OnSaveDefaultCommand()
        {
            var clientFilter = ParseClientFilter();
            AppMessages.StudentDefaultFilterMessage.Send(clientFilter);
            DialogResult = true;
        }

        /// <summary>
        /// Command for the Reset button
        /// </summary>
        public RelayCommand ResetCommand
        {
            get
            {
                if (_resetCommand == null)
                {
                    _resetCommand = new RelayCommand(
                        OnResetCommand);
                }
                return _resetCommand;
            }
        }

        private RelayCommand _resetCommand;

        private void OnResetCommand()
        {
            ResetAll();
        }

        /// <summary>
        /// Command for SelectionChanged event of StudentName SortDirection
        /// </summary>
        public RelayCommand StudentNameSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_studentNameSortDirectionSelectionChangedCommand == null)
                {
                    _studentNameSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnStudentNameSortDirectionSelectionChangedCommand);
                }
                return _studentNameSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _studentNameSortDirectionSelectionChangedCommand;

        private void OnStudentNameSortDirectionSelectionChangedCommand()
        {
            if (StudentNameSortDirectionIndex == 0)
            {
                if (StudentNameSortOrder != null)
                {
                    var currentInt = StudentNameSortOrder;
                    StudentNameSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (EnrollmentDateSortOrder != null && EnrollmentDateSortOrder > currentInt)
                    {
                        EnrollmentDateSortOrder = EnrollmentDateSortOrder - 1;
                    }
                    if (StatusSortOrder != null && StatusSortOrder > currentInt)
                    {
                        StatusSortOrder = StatusSortOrder - 1;
                    }
                }
            }
            else
            {
                if (StudentNameSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    StudentNameSortOrder = _currentMaxSortOrder;
                }
            }
        }

        /// <summary>
        /// Command for SelectionChanged event of EnrollmentDate SortDirection
        /// </summary>
        public RelayCommand EnrollmentDateSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_enrollmentDateSortDirectionSelectionChangedCommand == null)
                {
                    _enrollmentDateSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnEnrollmentDateSortDirectionSelectionChangedCommand);
                }
                return _enrollmentDateSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _enrollmentDateSortDirectionSelectionChangedCommand;

        private void OnEnrollmentDateSortDirectionSelectionChangedCommand()
        {
            if (EnrollmentDateSortDirectionIndex == 0)
            {
                if (EnrollmentDateSortOrder != null)
                {
                    var currentInt = EnrollmentDateSortOrder;
                    EnrollmentDateSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (StudentNameSortOrder != null && StudentNameSortOrder > currentInt)
                    {
                        StudentNameSortOrder = StudentNameSortOrder - 1;
                    }
                    if (StatusSortOrder != null && StatusSortOrder > currentInt)
                    {
                        StatusSortOrder = StatusSortOrder - 1;
                    }
                }
            }
            else
            {
                if (EnrollmentDateSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    EnrollmentDateSortOrder = _currentMaxSortOrder;
                }
            }
        }

        /// <summary>
        /// Command for SelectionChanged event of Status SortDirection
        /// </summary>
        public RelayCommand StatusSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_statusSortDirectionSelectionChangedCommand == null)
                {
                    _statusSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnStatusSortDirectionSelectionChangedCommand);
                }
                return _statusSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _statusSortDirectionSelectionChangedCommand;

        private void OnStatusSortDirectionSelectionChangedCommand()
        {
            if (StatusSortDirectionIndex == 0)
            {
                if (StatusSortOrder != null)
                {
                    var currentInt = StatusSortOrder;
                    StatusSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (StudentNameSortOrder != null && StudentNameSortOrder > currentInt)
                    {
                        StudentNameSortOrder = StudentNameSortOrder - 1;
                    }
                    if (EnrollmentDateSortOrder != null && EnrollmentDateSortOrder > currentInt)
                    {
                        EnrollmentDateSortOrder = EnrollmentDateSortOrder - 1;
                    }
                }
            }
            else
            {
                if (StatusSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    StatusSortOrder = _currentMaxSortOrder;
                }
            }
        }

        #endregion "Public Commands"

        #region "Private Methods"

        /// <summary>
        /// Message handler for StudentFilterMessage
        /// </summary>
        /// <param name="notification"></param>
        private void OnStudentFilterMessage(NotificationMessageAction<ClientFilter<Student>> notification)
        {
            _notification = notification;
        }

        /// <summary>
        /// Reset all to initial settings
        /// </summary>
        private void ResetAll()
        {
            StudentName = string.Empty;
            EnrollmentDate = null;
            Status = null;

            StudentNameSortOrder = null;
            EnrollmentDateSortOrder = null;
            StatusSortOrder = null;

            StudentNameSortDirectionIndex = 0;
            EnrollmentDateSortDirectionIndex = 0;
            StatusSortDirectionIndex = 0;

            JointCondition1Index = 0;
            JointCondition2Index = 0;

            EnrollmentDateCompareOperatorIndex = 0;
            StatusCompareOperatorIndex = 0;

            _currentMaxSortOrder = 0;
        }

        /// <summary>
        /// Parse and create ClientFilter object
        /// </summary>
        /// <returns></returns>
        private ClientFilter<Student> ParseClientFilter()
        {
            var clientFilter = new ClientFilter<Student>();
            // dynamically build WhereExpression
            // filter by Student Name
            if (!string.IsNullOrEmpty(StudentName))
            {
                clientFilter = clientFilter.And(n => n.Name.Contains(StudentName));
            }

            // filter by Student Enrollment Date
            var clientFilter1 = new ClientFilter<Student>();
            if (EnrollmentDate != null)
            {
                switch (EnrollmentDateCompareOperatorIndex)
                {
                    case 0: // >=
                        clientFilter1 = clientFilter1.And(
                            n => n.EnrollmentDate >=
                                 ((DateTime) EnrollmentDate).Date);
                        break;
                    case 1: // >
                        clientFilter1 = clientFilter1.And(
                            n => n.EnrollmentDate >=
                                 ((DateTime) EnrollmentDate).Date.AddDays(1));
                        break;
                    case 2: // =
                        clientFilter1 = clientFilter1.And(
                            n => n.EnrollmentDate >=
                                 ((DateTime) EnrollmentDate).Date &&
                                 n.EnrollmentDate < ((DateTime) EnrollmentDate).Date.AddDays(1));
                        break;
                    case 3: // <
                        clientFilter1 = clientFilter1.And(
                            n => n.EnrollmentDate <
                                 ((DateTime) EnrollmentDate).Date);
                        break;
                    case 4: // <=
                        clientFilter1 = clientFilter1.And(
                            n => n.EnrollmentDate <
                                 ((DateTime) EnrollmentDate).Date.AddDays(1));
                        break;
                }
            }
            clientFilter = JointCondition1Index == 0
                               ? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
                               : clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or

            // filter by Student Status
            clientFilter1 = new ClientFilter<Student>();
            if (Status != null)
            {
                switch (StatusCompareOperatorIndex)
                {
                    case 0: // =
                        clientFilter1 =
                            clientFilter1.And(n => n.Status == (StatusEnum) Status);
                        break;
                    case 1: // !=
                        clientFilter1 =
                            clientFilter1.And(n => n.Status != (StatusEnum) Status);
                        break;
                }
            }
            clientFilter = JointCondition2Index == 0
                               ? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
                               : clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or

            // dynamically build SortExpressions
            if (StudentNameSortOrder != null && StudentNameSortOrder == 1)
            {
                clientFilter = StudentNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Name); // Descending
            }
            else if (EnrollmentDateSortOrder != null && EnrollmentDateSortOrder == 1)
            {
                clientFilter = EnrollmentDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EnrollmentDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EnrollmentDate); // Descending
            }
            else if (StatusSortOrder != null && StatusSortOrder == 1)
            {
                clientFilter = StatusSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Status) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Status); // Descending
            }

            if (StudentNameSortOrder != null && StudentNameSortOrder == 2)
            {
                clientFilter = StudentNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Name); // Descending
            }
            else if (EnrollmentDateSortOrder != null && EnrollmentDateSortOrder == 2)
            {
                clientFilter = EnrollmentDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EnrollmentDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EnrollmentDate); // Descending
            }
            else if (StatusSortOrder != null && StatusSortOrder == 2)
            {
                clientFilter = StatusSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Status) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Status); // Descending
            }

            if (StudentNameSortOrder != null && StudentNameSortOrder == 3)
            {
                clientFilter = StudentNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Name); // Descending
            }
            else if (EnrollmentDateSortOrder != null && EnrollmentDateSortOrder == 3)
            {
                clientFilter = EnrollmentDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EnrollmentDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EnrollmentDate); // Descending
            }
            else if (StatusSortOrder != null && StatusSortOrder == 3)
            {
                clientFilter = StatusSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Status) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Status); // Descending
            }

            // the default sort order is PersonId
            if (clientFilter.SortExpressions.Count == 0)
            {
                clientFilter = clientFilter.OrderBy(n => n.PersonId);
            }
            return clientFilter;
        }

        #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)


Written By
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

Comments and Discussions