using MeterVision.db; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace MeterVision.Stand { /// /// SelecteStationDlg.xaml 的交互逻辑 /// public partial class SelecteStationDlg : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public ObservableCollection StationList1 { get; set; } public ObservableCollection StationList2 { get; set; } private ICollectionView _stationListView1; public ICollectionView StationListView1 { get => _stationListView1; set { _stationListView1 = value; OnPropertyChanged(nameof(StationListView1)); } } private ICollectionView _stationListView2; public ICollectionView StationListView2 { get => _stationListView2; set { _stationListView2 = value; OnPropertyChanged(nameof(StationListView2)); } } //public List StationList1 { get; set; } //public List StationList2 { get; set; } public int TotalStationCount1 { get; set; } public int TotalStationCount2 { get; set; } public TStation SelectedStationItem1 { get; set; } public TStation SelectedStationItem2 { get; set; } public string StationId { get; set; } private List _allStationList; public SelecteStationDlg(List stations) { InitializeComponent(); StationId = string.Empty; //StationList1 = new List(); //new ObservableCollection(); //StationList2 = new List(); //new ObservableCollection(); StationList1 = new ObservableCollection(); StationList2 = new ObservableCollection(); foreach (var item in stations) { StationList1.Add(item); } //StationList1.AddRange(stations); _allStationList = new List(); _allStationList.AddRange(stations); //dgStation1.ItemsSource = StationList1; StationListView1 = CollectionViewSource.GetDefaultView(StationList1); StationListView1.SortDescriptions.Add(new SortDescription("StationId", ListSortDirection.Ascending)); dgStation1.ItemsSource = StationListView1; //dgStation2.ItemsSource = StationList2; StationListView2 = CollectionViewSource.GetDefaultView(StationList2); StationListView2.SortDescriptions.Add(new SortDescription("StationId", ListSortDirection.Ascending)); dgStation2.ItemsSource = StationListView2; //TotalStationCount1 = StationList1.Count; //TotalStationCount2 = StationList2.Count; this.DataContext = this; } private void BtnViewImage_Click(object sender, RoutedEventArgs e) { //GetMinSampleTimeDetail Button button = sender as Button; if (button == null) return; DataGridRow row = FindAncestor(button); if (row == null) return; //获取当前行绑定的数据项 var selectedItem = row.Item as TStation; if (selectedItem == null) return; TStandDetail standDetail = DBStand.GetMinSampleTimeDetail(selectedItem.StandId, selectedItem.StationId); if (standDetail == null) return; if (!File.Exists(standDetail.SrcImage)) return; var dialog = new imgWindow(standDetail.SrcImage) { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }; dialog.Show(); } private void BtnAddStation_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; if (button == null) return; DataGridRow row = FindAncestor(button); if (row == null) return; //获取当前行绑定的数据项 var selectedItem = row.Item as TStation; StationList1.Remove(selectedItem); StationList2.Add(selectedItem); StationListView2.Refresh(); //刷新,以确保排序 } private void BtnRemoveStation_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; if (button == null) return; DataGridRow row = FindAncestor(button); if (row == null) return; //获取当前行绑定的数据项 var selectedItem = row.Item as TStation; StationList2.Remove(selectedItem); StationList1.Add(selectedItem); StationListView1.Refresh(); //刷新CollectionView,以确保排序 } private void BtnOK_Click(object sender, RoutedEventArgs e) { if(StationList2.Count <= 0) { MessageBox.Show("请最少选择一个站点"); return; } DialogResult = true; this.Close(); } private void BtnClose_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; this.Close(); } private void BtnQuery_Click(object sender, RoutedEventArgs e) { StationList1.Clear(); string stationId = StationId.Trim(); foreach(var item in _allStationList) { if (item.StationId.Contains(stationId)) { StationList1.Add(item); } } StationListView1.Refresh(); } private static T FindAncestor(DependencyObject dependencyObject) where T : DependencyObject { var parent = VisualTreeHelper.GetParent(dependencyObject); if (parent == null) return null; var parentT = parent as T; return parentT ?? FindAncestor(parent); } //------------------------------------------------------------------------------- } }