| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using GalaSoft.MvvmLight;
- using GalaSoft.MvvmLight.Command;
- using Muchinfo.PC.Common.Extensions;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Threading.Tasks;
- namespace Muchinfo.MTPClient.Infrastructure.DataPager
- {
- public abstract class QueryViewModelBase<T> : ViewModelBase
- {
- #region Fields
- private ObservableCollection<T> _dataGridItemsSource;
- private T _currentDataGridItem;
- private int _pageSize;
- private int _pageIndex;
- private int _itemCount;
- #endregion
- #region Properties
- /// <summary>
- /// DataGrid绑定集合
- /// </summary>
- public ObservableCollection<T> DataGridItemsSource
- {
- get { return _dataGridItemsSource; }
- set
- {
- Set(() => DataGridItemsSource, ref _dataGridItemsSource, value);
- }
- }
- /// <summary>
- /// DataGrid当前选中项
- /// </summary>
- public T CurrentDataGridItem
- {
- get { return _currentDataGridItem; }
- set { Set(() => CurrentDataGridItem, ref _currentDataGridItem, value); }
- }
- /// <summary>
- /// 总记录条数
- /// </summary>
- public int ItemCount
- {
- get { return _itemCount; }
- set { Set(() => ItemCount, ref _itemCount, value); }
- }
- /// <summary>
- /// 每页条数
- /// </summary>
- public int PageSize
- {
- get { return _pageSize; }
- set { Set(() => PageSize, ref _pageSize, value); }
- }
- /// <summary>
- /// 当前页
- /// </summary>
- public int PageIndex
- {
- get { return _pageIndex; }
- set { Set(() => PageIndex, ref _pageIndex, value); }
- }
- /// <summary>
- /// 重置命令
- /// </summary>
- public RelayCommand ResetCommand
- {
- get
- {
- return new RelayCommand(Reset);
- }
- }
- private bool isQuery = false;
- /// <summary>
- /// 查询命令
- /// </summary>
- public RelayCommand SearchCommand
- {
- get
- {
- return new RelayCommand(() =>
- {
- if (QuerCheck())
- {
- //通过PageIndex触发PagerSearchCommand
- if (PageIndex == 0) PageIndex = -1;
- PageIndex = 0;
- }
- }, () => { return !isQuery; });
- }
- }
- /// <summary>
- /// 分页命令
- /// </summary>
- public RelayCommand PagerSearchCommand
- {
- get
- {
- return new RelayCommand(() => Task.Factory.TryStartNew(() =>
- {
- isQuery = true;
- if (QuerCheck())
- {
- int totalCount;
- var result = Query(out totalCount);
- ItemCount = totalCount;
- DataGridItemsSource = result.ToObservableCollection();
- if (DataGridItemsSource != null && DataGridItemsSource.Any())
- {
- CurrentDataGridItem = DataGridItemsSource[0];
- }
- }
- isQuery = false;
- }, () => { isQuery = false; }));
- }
- }
- #endregion
- #region Constructor
- protected QueryViewModelBase()
- {
- PageSize = 15;
- RegisterMessage();
- }
- #endregion
- #region Methods
- /// <summary>
- /// 查询数据集合
- /// </summary>
- /// <returns>IEnumerable{`0}.</returns>
- protected abstract IEnumerable<T> Query(out int totalCount);
- /// <summary>
- /// 查询检查
- /// </summary>
- /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
- protected virtual bool QuerCheck()
- {
- return true;
- }
- /// <summary>
- /// 重置查询条件
- /// </summary>
- protected virtual void Reset() { }
- /// <summary>
- /// 初始化
- /// </summary>
- protected virtual void Initialize()
- {
- Reset();
- SearchCommand.Execute(string.Empty);
- }
- /// <summary>
- /// 注册消息
- /// </summary>
- protected virtual void RegisterMessage() { }
- #endregion
- }
- }
|