珂珂的个人博客 - 一个程序猿的个人网站

WPF的MVVM实例(二)-ViewModel基类

[原]WPF的MVVM实例(一)

[原]WPF的MVVM实例(二)-ViewModel基类

[原]WPF的MVVM实例(三)-ViewModel实现类

[原]WPF的MVVM实例(四)-列表页

[原]WPF的MVVM实例(五)-编辑页

using MyList.Common;
using MyList.ViewModel.Util;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace MyList.ViewModel
{
    public abstract class BaseViewModel<T> : INotifyPropertyChanged where T : class
    {
        protected Dispatcher _dispatcher;
        private string _searchString;

        #region 构造函数
        public BaseViewModel(Dispatcher dispatcher)
        {
            this._dispatcher = dispatcher;
        }

        public BaseViewModel()
        {

        }
        #endregion

        #region 属性
        public DataGrid MdataGrid
        {
            get;
            set;
        }
        /// <summary>
        /// 搜索窗口
        /// </summary>
        public Window MSearchWindow
        {
            get;
            set;
        }
        public ObservableCollection<T> QueryData
        {
            get;
            set;
        }

        public ObservableCollection<T> SaveData
        {
            get;
            set;
        }

        /// <summary>
        /// Loading动画显示状态
        /// </summary>
        private Visibility _loadingVisibility = Visibility.Collapsed;
        public Visibility LoadingVisibility
        {
            get
            {
                return _loadingVisibility;
            }
            set
            {
                if (_loadingVisibility != value)
                {
                    _loadingVisibility = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("LoadingVisibility"));
                    }
                }
            }
        }
        #endregion

        #region 命令

        #region 增删改查
        ICommand _queryCommand;
        ICommand _saveCommand;
        ICommand _deleteCommand;

        public ICommand QueryCommand
        {
            get
            {
                if (this._queryCommand == null)
                {
                    this._queryCommand = new RelayCommand(
                        (obj) => this.Query(obj),
                        (obj) => this.CanQuery(obj));
                }

                return this._queryCommand;
            }
        }
        public ICommand SaveCommand
        {
            get
            {
                if (this._saveCommand == null)
                {
                    this._saveCommand = new RelayCommand(
                        (obj) => this.Save(obj),
                        (obj) => this.CanSave(obj));
                }

                return this._saveCommand;
            }
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (this._deleteCommand == null)
                {
                    this._deleteCommand = new RelayCommand(
                      (obj) => this.Delete(obj),
                      (obj) => this.CanDelete(obj));
                }

                return this._deleteCommand;
            }
        }

        private bool CanSave(object obj)
        {
            return true;
        }

        private bool CanDelete(object obj)
        {
            if (this.MdataGrid != null && this.MdataGrid.SelectedItems.Count > 0)
            {
                return true;
            }
            return false;
        }

        private bool CanQuery(object obj)
        {
            return true;
        }
        #endregion

        #region 搜索
        private ICommand _searchCommand;
        private ICommand _searchNextCommand;

        public ICommand SearchCommand
        {
            get
            {
                if (this._searchCommand == null)
                {
                    this._searchCommand = new RelayCommand(
                        (obj) => this.Search(obj),
                        (obj) => this.CanSearch(obj));
                }

                return this._searchCommand;
            }
        }

        private void Search(object obj)
        {
            if (MSearchWindow.ShowDialog() == true)
            {
                _searchString = (string)MSearchWindow.Tag;
                int index = DataGridHelper.GetSearchResultIndex(this.MdataGrid, _searchString);
                if (index != -1)
                {
                    MdataGrid.SelectedIndex = index;
                }
                else
                {
                    MessageBox.Show("没有匹配的记录");
                }
            }

            //重新构造窗体 否则无法再调用ShowDialog()
            MSearchWindow = (Window)Activator.CreateInstance(MSearchWindow.GetType());
        }

        private bool CanSearch(object obj)
        {
            if (MdataGrid != null && MSearchWindow != null)
            {
                return true;
            }
            return false;
        }

        public ICommand SearchNextCommand
        {
            get
            {
                if (this._searchNextCommand == null)
                {
                    this._searchNextCommand = new RelayCommand(
                        (obj) => this.SearchNext(obj),
                        (obj) => this.CanSearchNext(obj));
                }

                return this._searchNextCommand;
            }
        }

        private void SearchNext(object obj)
        {
            int index = DataGridHelper.GetSearchResultIndex(this.MdataGrid, _searchString);
            if (index != -1)
            {
                MdataGrid.SelectedIndex = index;
            }
            else
            {
                MessageBox.Show("没有匹配的记录");
            }
        }

        private bool CanSearchNext(object obj)
        {
            if (MdataGrid != null && _searchString != null)
            {
                return true;
            }
            return false;
        }
        #endregion

        private ICommand _exportCommand;
        public ICommand ExportCommand
        {
            get
            {
                if (this._exportCommand == null)
                {
                    this._exportCommand = new RelayCommand(
                        (obj) => this.Export(obj),
                        (obj) => this.CanExport(obj));
                }

                return this._exportCommand;
            }
        }

        private bool CanExport(object obj)
        {
            return (this.MdataGrid != null && MdataGrid.CurrentCell != null);
        }

        #endregion

        protected abstract void Save(object obj);

        protected abstract void Delete(object obj);

        protected abstract void Query(object obj);

        protected abstract void Export(object obj);


        public event PropertyChangedEventHandler PropertyChanged;
    }
}



上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧