WPF 精修篇 DataGrid 筛选

2021-01-23 06:12

阅读:393

标签:ble   reg   ado   col   comment   def   ring   key   vat   

原文:WPF 精修篇 DataGrid 筛选

DataGrid也可以分组 但是用的地方不多 就没写

筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选

技术图片

贴码

  1. "datagrid" AutoGenerateColumns="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
  2. "{Binding Name}" Header="Name">
  3. "{Binding Address}" Header="Address">
  4. "{Binding Age}" Header="Age">
  5. "sort" Content="排序" HorizontalAlignment="Left" Margin="466,300,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>
  6. "TxT" HorizontalAlignment="Left" Height="25" Margin="402,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="115" TextChanged="TextBox_TextChanged"/>
  7. "Left" Height="20" Margin="336,8,0,0" TextWrapping="Wrap" Text="筛选内容" VerticalAlignment="Top" Width="61"/>
  1. public MainWindow()
  2. {
  3. InitializeComponent();
  4. Preson = new ObservableCollection() {
  5. new Preson() { Name = "慧哥1", Address = "大连1", Age = 31 },
  6. new Preson() { Name = "慧哥2", Address = "大连2", Age = 32 },
  7. new Preson() { Name = "慧哥3", Address = "大连3", Age = 33 },
  8. new Preson() { Name = "慧哥4", Address = "大连4", Age = 34 },
  9. new Preson() { Name = "慧哥5", Address = "大连5", Age = 35 },
  10. new Preson() { Name = "123", Address = "大连6", Age = 12 },
  11. new Preson() { Name = "444", Address = "大连7", Age = 14 },
  12. new Preson() { Name = "222", Address = "大连8", Age = 33 },
  13. new Preson() { Name = "1312", Address = "大连9", Age = 22 }
  14. };
  15. datagrid.ItemsSource = Preson;
  16. }
  17. public ObservableCollection Preson
  18. {
  19. get { return (ObservableCollection)GetValue(PresonProperty); }
  20. set { SetValue(PresonProperty, value); }
  21. }
  22. // Using a DependencyProperty as the backing store for Preson. This enables animation, styling, binding, etc...
  23. public static readonly DependencyProperty PresonProperty =
  24. DependencyProperty.Register("Preson", typeof(ObservableCollection), typeof(MainWindow), new PropertyMetadata(null));
  25. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  26. {
  27. var cvs = CollectionViewSource.GetDefaultView(datagrid.ItemsSource);
  28. if(cvs!=null&&cvs.CanSort)
  29. {
  30. cvs.SortDescriptions.Clear();
  31. if (sort.IsChecked == true)
  32. {
  33. cvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Age", System.ComponentModel.ListSortDirection.Descending));
  34. }
  35. }
  36. }
  37. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  38. {
  39. var cvs = CollectionViewSource.GetDefaultView(datagrid.ItemsSource);
  40. if (cvs != null && cvs.CanFilter)
  41. {
  42. cvs.Filter = OnFilterApplied;
  43. }
  44. }
  45. private bool OnFilterApplied(object obj)
  46. {
  47. if (obj is Preson)
  48. {
  49. var text = TxT.Text.ToLower();
  50. return (obj as Preson).Name.ToLower().Contains(text)
  51. || (obj as Preson).Address.ToLower().Contains(text);
  52. }
  53. return false;
  54. }
  55. }

啊~ 这里包含排序 那就包含吧~~

WPF 精修篇 DataGrid 筛选

标签:ble   reg   ado   col   comment   def   ring   key   vat   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12075454.html


评论


亲,登录后才可以留言!