UCVpsGrid.xaml.cs 8.8 KB

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