QueryViewModelBase.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Command;
  3. using Muchinfo.PC.Common.Extensions;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace Muchinfo.MTPClient.Infrastructure.DataPager
  9. {
  10. public abstract class QueryViewModelBase<T> : ViewModelBase
  11. {
  12. #region Fields
  13. private ObservableCollection<T> _dataGridItemsSource;
  14. private T _currentDataGridItem;
  15. private int _pageSize;
  16. private int _pageIndex;
  17. private int _itemCount;
  18. #endregion
  19. #region Properties
  20. /// <summary>
  21. /// DataGrid绑定集合
  22. /// </summary>
  23. public ObservableCollection<T> DataGridItemsSource
  24. {
  25. get { return _dataGridItemsSource; }
  26. set
  27. {
  28. Set(() => DataGridItemsSource, ref _dataGridItemsSource, value);
  29. }
  30. }
  31. /// <summary>
  32. /// DataGrid当前选中项
  33. /// </summary>
  34. public T CurrentDataGridItem
  35. {
  36. get { return _currentDataGridItem; }
  37. set { Set(() => CurrentDataGridItem, ref _currentDataGridItem, value); }
  38. }
  39. /// <summary>
  40. /// 总记录条数
  41. /// </summary>
  42. public int ItemCount
  43. {
  44. get { return _itemCount; }
  45. set { Set(() => ItemCount, ref _itemCount, value); }
  46. }
  47. /// <summary>
  48. /// 每页条数
  49. /// </summary>
  50. public int PageSize
  51. {
  52. get { return _pageSize; }
  53. set { Set(() => PageSize, ref _pageSize, value); }
  54. }
  55. /// <summary>
  56. /// 当前页
  57. /// </summary>
  58. public int PageIndex
  59. {
  60. get { return _pageIndex; }
  61. set { Set(() => PageIndex, ref _pageIndex, value); }
  62. }
  63. /// <summary>
  64. /// 重置命令
  65. /// </summary>
  66. public RelayCommand ResetCommand
  67. {
  68. get
  69. {
  70. return new RelayCommand(Reset);
  71. }
  72. }
  73. private bool isQuery = false;
  74. /// <summary>
  75. /// 查询命令
  76. /// </summary>
  77. public RelayCommand SearchCommand
  78. {
  79. get
  80. {
  81. return new RelayCommand(() =>
  82. {
  83. if (QuerCheck())
  84. {
  85. //通过PageIndex触发PagerSearchCommand
  86. if (PageIndex == 0) PageIndex = -1;
  87. PageIndex = 0;
  88. }
  89. }, () => { return !isQuery; });
  90. }
  91. }
  92. /// <summary>
  93. /// 分页命令
  94. /// </summary>
  95. public RelayCommand PagerSearchCommand
  96. {
  97. get
  98. {
  99. return new RelayCommand(() => Task.Factory.TryStartNew(() =>
  100. {
  101. isQuery = true;
  102. if (QuerCheck())
  103. {
  104. int totalCount;
  105. var result = Query(out totalCount);
  106. ItemCount = totalCount;
  107. DataGridItemsSource = result.ToObservableCollection();
  108. if (DataGridItemsSource != null && DataGridItemsSource.Any())
  109. {
  110. CurrentDataGridItem = DataGridItemsSource[0];
  111. }
  112. }
  113. isQuery = false;
  114. }, () => { isQuery = false; }));
  115. }
  116. }
  117. #endregion
  118. #region Constructor
  119. protected QueryViewModelBase()
  120. {
  121. PageSize = 15;
  122. RegisterMessage();
  123. }
  124. #endregion
  125. #region Methods
  126. /// <summary>
  127. /// 查询数据集合
  128. /// </summary>
  129. /// <returns>IEnumerable{`0}.</returns>
  130. protected abstract IEnumerable<T> Query(out int totalCount);
  131. /// <summary>
  132. /// 查询检查
  133. /// </summary>
  134. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  135. protected virtual bool QuerCheck()
  136. {
  137. return true;
  138. }
  139. /// <summary>
  140. /// 重置查询条件
  141. /// </summary>
  142. protected virtual void Reset() { }
  143. /// <summary>
  144. /// 初始化
  145. /// </summary>
  146. protected virtual void Initialize()
  147. {
  148. Reset();
  149. SearchCommand.Execute(string.Empty);
  150. }
  151. /// <summary>
  152. /// 注册消息
  153. /// </summary>
  154. protected virtual void RegisterMessage() { }
  155. #endregion
  156. }
  157. }