UCVpsGrid.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using MeterVision.Config;
  2. using MeterVision.db;
  3. using MeterVision.model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace MeterVision.Station
  21. {
  22. /// <summary>
  23. /// UCVpsGrid.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class UCVpsGrid : UserControl, INotifyPropertyChanged
  26. {
  27. public event PropertyChangedEventHandler PropertyChanged;
  28. protected void OnPropertyChanged(string propertyName)
  29. {
  30. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  31. }
  32. public ObservableCollection<VPatchStation> StationItemList { get; set; }
  33. public event Action<VPatchStation> OnItemChanged;
  34. private VPatchStation _selectedItem;
  35. public VPatchStation SelectedItem
  36. {
  37. get => _selectedItem;
  38. set
  39. {
  40. if(_selectedItem != value)
  41. {
  42. _selectedItem = value;
  43. OnPropertyChanged(nameof(SelectedItem));
  44. OnItemChanged?.Invoke(value);
  45. }
  46. }
  47. }
  48. //要查找的站点ID
  49. private string _findStationId;
  50. public string FindStationId
  51. {
  52. get => _findStationId;
  53. private set
  54. {
  55. if (_findStationId != value)
  56. {
  57. _findStationId = value;
  58. }
  59. }
  60. }
  61. private string _patchId;
  62. public string PatchId
  63. {
  64. get => _patchId;
  65. private set
  66. {
  67. if (_patchId != value)
  68. {
  69. _patchId = value;
  70. }
  71. }
  72. }
  73. public void ChangeFind(string stationId, string patchId)
  74. {
  75. bool blChangeFindStationId = false;
  76. bool blChangePatchId = false;
  77. if (stationId != _findStationId)
  78. {
  79. FindStationId = stationId;
  80. blChangeFindStationId = true;
  81. }
  82. if (patchId != _patchId)
  83. {
  84. PatchId = patchId;
  85. blChangePatchId = true;
  86. }
  87. if (blChangeFindStationId || blChangePatchId)
  88. {
  89. StationPage.InitDefaulValue();
  90. LoadStationItemList(true);
  91. }
  92. }
  93. public CfginiItem mConfigItem { get; set; }
  94. public PageModel StationPage { get; set; }
  95. private int _totalRecords;
  96. public int TotalRecords
  97. {
  98. get => _totalRecords;
  99. set
  100. {
  101. if (_totalRecords != value)
  102. {
  103. _totalRecords = value;
  104. OnPropertyChanged(nameof(TotalRecords));
  105. }
  106. }
  107. }
  108. public UCVpsGrid()
  109. {
  110. InitializeComponent();
  111. StationItemList = new ObservableCollection<VPatchStation>();
  112. dgStation.ItemsSource = StationItemList;
  113. mConfigItem = CfginiItem.GetConfigItem();
  114. StationPage = new PageModel
  115. {
  116. PageSize = mConfigItem.PatchPageSize,
  117. PageNumber = 1
  118. };
  119. mConfigItem.OnPatchPageSizeChanged += MConfigItem_OnPatchPageSizeChanged;
  120. this.DataContext = this;
  121. }
  122. private void MConfigItem_OnPatchPageSizeChanged(int pageSize)
  123. {
  124. StationPage.InitDefaulValue(pageSize);
  125. LoadStationItemList(true);
  126. }
  127. private async void LoadStationItemList(bool scrollTop)
  128. {
  129. StationItemList.Clear();
  130. if (FindStationId == null)
  131. {
  132. return;
  133. }
  134. try
  135. {
  136. var result = await Task.Run(() =>
  137. {
  138. return DBPatch.GetPagedPatchStations(StationPage.PageNumber, StationPage.PageSize,PatchId);
  139. });
  140. //更新分页信息
  141. TotalRecords = result.Item1;
  142. StationPage.PageCount = result.Item2;
  143. List<VPatchStation> stationList = result.Item3;
  144. //在主线中更新数据
  145. Application.Current.Dispatcher.Invoke(() =>
  146. {
  147. int index = 0;
  148. foreach (VPatchStation station in stationList)
  149. {
  150. index++;
  151. //StationItem item = new StationItem(station);
  152. //item.Index = index + ((StationPage.PageNumber - 1) * StationPage.PageSize);
  153. //StationItemList.Add(item);
  154. station.Index = index + ((StationPage.PageNumber - 1) * StationPage.PageSize);
  155. }
  156. SelectedItem = null;
  157. if (StationItemList.Count > 0 && scrollTop)
  158. {
  159. dgStation.ScrollIntoView(dgStation.Items[0]);
  160. }
  161. });
  162. }
  163. catch (Exception ex)
  164. {
  165. MessageBox.Show(Application.Current.MainWindow,
  166. $"加载数据是发生错误:{ex.Message}", "错误",
  167. MessageBoxButton.OK, MessageBoxImage.Error);
  168. }
  169. SelectedItem = null;
  170. }
  171. public void NextPage()
  172. {
  173. if (StationPage.PageNumber < StationPage.PageCount)
  174. {
  175. StationPage.PageNumber += 1;
  176. LoadStationItemList(true);
  177. }
  178. }
  179. public void PrePage()
  180. {
  181. if (StationPage.PageNumber > 1)
  182. {
  183. StationPage.PageNumber -= 1;
  184. LoadStationItemList(true);
  185. }
  186. }
  187. public void FirstPage()
  188. {
  189. if (StationPage.PageNumber > 1)
  190. {
  191. StationPage.PageNumber = 1;
  192. LoadStationItemList(true);
  193. }
  194. }
  195. public void LastPage()
  196. {
  197. if (StationPage.PageNumber < StationPage.PageCount)
  198. {
  199. StationPage.PageNumber = StationPage.PageCount;
  200. LoadStationItemList(true);
  201. }
  202. }
  203. public void SpeciPage(int pageNumber)
  204. {
  205. if (pageNumber != StationPage.PageNumber &&
  206. pageNumber > 0 && pageNumber <= StationPage.PageCount)
  207. {
  208. StationPage.PageNumber = pageNumber;
  209. LoadStationItemList(true);
  210. }
  211. }
  212. //---------------------------------------------------------------------
  213. }
  214. }