SelecteStationDlg.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using MeterVision.db;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace MeterVision.Stand
  19. {
  20. /// <summary>
  21. /// SelecteStationDlg.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class SelecteStationDlg : Window, INotifyPropertyChanged
  24. {
  25. public event PropertyChangedEventHandler PropertyChanged;
  26. protected void OnPropertyChanged(string propertyName)
  27. {
  28. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  29. }
  30. public ObservableCollection<TStation> StationList1 { get; set; }
  31. public ObservableCollection<TStation> StationList2 { get; set; }
  32. private ICollectionView _stationListView1;
  33. public ICollectionView StationListView1
  34. {
  35. get => _stationListView1;
  36. set
  37. {
  38. _stationListView1 = value;
  39. OnPropertyChanged(nameof(StationListView1));
  40. }
  41. }
  42. private ICollectionView _stationListView2;
  43. public ICollectionView StationListView2
  44. {
  45. get => _stationListView2;
  46. set
  47. {
  48. _stationListView2 = value;
  49. OnPropertyChanged(nameof(StationListView2));
  50. }
  51. }
  52. //public List<TStation> StationList1 { get; set; }
  53. //public List<TStation> StationList2 { get; set; }
  54. public int TotalStationCount1 { get; set; }
  55. public int TotalStationCount2 { get; set; }
  56. public TStation SelectedStationItem1 { get; set; }
  57. public TStation SelectedStationItem2 { get; set; }
  58. public string StationId { get; set; }
  59. private List<TStation> _allStationList;
  60. public SelecteStationDlg(List<TStation> stations)
  61. {
  62. InitializeComponent();
  63. StationId = string.Empty;
  64. //StationList1 = new List<TStation>(); //new ObservableCollection<TStation>();
  65. //StationList2 = new List<TStation>(); //new ObservableCollection<TStation>();
  66. StationList1 = new ObservableCollection<TStation>();
  67. StationList2 = new ObservableCollection<TStation>();
  68. foreach (var item in stations)
  69. {
  70. StationList1.Add(item);
  71. }
  72. //StationList1.AddRange(stations);
  73. _allStationList = new List<TStation>();
  74. _allStationList.AddRange(stations);
  75. //dgStation1.ItemsSource = StationList1;
  76. StationListView1 = CollectionViewSource.GetDefaultView(StationList1);
  77. StationListView1.SortDescriptions.Add(new SortDescription("StationId", ListSortDirection.Ascending));
  78. dgStation1.ItemsSource = StationListView1;
  79. //dgStation2.ItemsSource = StationList2;
  80. StationListView2 = CollectionViewSource.GetDefaultView(StationList2);
  81. StationListView2.SortDescriptions.Add(new SortDescription("StationId", ListSortDirection.Ascending));
  82. dgStation2.ItemsSource = StationListView2;
  83. //TotalStationCount1 = StationList1.Count;
  84. //TotalStationCount2 = StationList2.Count;
  85. this.DataContext = this;
  86. }
  87. private void BtnViewImage_Click(object sender, RoutedEventArgs e)
  88. {
  89. //GetMinSampleTimeDetail
  90. Button button = sender as Button;
  91. if (button == null) return;
  92. DataGridRow row = FindAncestor<DataGridRow>(button);
  93. if (row == null) return;
  94. //获取当前行绑定的数据项
  95. var selectedItem = row.Item as TStation;
  96. if (selectedItem == null) return;
  97. TStandDetail standDetail = DBStand.GetMinSampleTimeDetail(selectedItem.StandId, selectedItem.StationId);
  98. if (standDetail == null) return;
  99. if (!File.Exists(standDetail.SrcImage)) return;
  100. var dialog = new imgWindow(standDetail.SrcImage)
  101. {
  102. Owner = this,
  103. WindowStartupLocation = WindowStartupLocation.CenterOwner
  104. };
  105. dialog.Show();
  106. }
  107. private void BtnAddStation_Click(object sender, RoutedEventArgs e)
  108. {
  109. Button button = sender as Button;
  110. if (button == null) return;
  111. DataGridRow row = FindAncestor<DataGridRow>(button);
  112. if (row == null) return;
  113. //获取当前行绑定的数据项
  114. var selectedItem = row.Item as TStation;
  115. StationList1.Remove(selectedItem);
  116. StationList2.Add(selectedItem);
  117. StationListView2.Refresh(); //刷新,以确保排序
  118. }
  119. private void BtnRemoveStation_Click(object sender, RoutedEventArgs e)
  120. {
  121. Button button = sender as Button;
  122. if (button == null) return;
  123. DataGridRow row = FindAncestor<DataGridRow>(button);
  124. if (row == null) return;
  125. //获取当前行绑定的数据项
  126. var selectedItem = row.Item as TStation;
  127. StationList2.Remove(selectedItem);
  128. StationList1.Add(selectedItem);
  129. StationListView1.Refresh(); //刷新CollectionView,以确保排序
  130. }
  131. private void BtnOK_Click(object sender, RoutedEventArgs e)
  132. {
  133. if(StationList2.Count <= 0)
  134. {
  135. MessageBox.Show("请最少选择一个站点");
  136. return;
  137. }
  138. DialogResult = true;
  139. this.Close();
  140. }
  141. private void BtnClose_Click(object sender, RoutedEventArgs e)
  142. {
  143. this.DialogResult = false;
  144. this.Close();
  145. }
  146. private void BtnQuery_Click(object sender, RoutedEventArgs e)
  147. {
  148. StationList1.Clear();
  149. string stationId = StationId.Trim();
  150. foreach(var item in _allStationList)
  151. {
  152. if (item.StationId.Contains(stationId))
  153. {
  154. StationList1.Add(item);
  155. }
  156. }
  157. StationListView1.Refresh();
  158. }
  159. private static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
  160. {
  161. var parent = VisualTreeHelper.GetParent(dependencyObject);
  162. if (parent == null) return null;
  163. var parentT = parent as T;
  164. return parentT ?? FindAncestor<T>(parent);
  165. }
  166. //-------------------------------------------------------------------------------
  167. }
  168. }