WPF 精修篇 DataGrid 筛选

原文:WPF 精修篇 DataGrid 筛选

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

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

WPF 精修篇 DataGrid 筛选

贴码

  1. <DataGrid x:Name="datagrid" AutoGenerateColumns="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
  2. <DataGrid.Columns>
  3. <DataGridTextColumn Binding="{Binding Name}" Header="Name"></DataGridTextColumn>
  4. <DataGridTextColumn Binding="{Binding Address}" Header="Address"></DataGridTextColumn>
  5. <DataGridTextColumn Binding="{Binding Age}" Header="Age"></DataGridTextColumn>
  6. </DataGrid.Columns>
  7. </DataGrid>
  8. <CheckBox x:Name="sort" Content="排序" HorizontalAlignment="Left" Margin="466,300,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>
  9. <TextBox x:Name="TxT" HorizontalAlignment="Left" Height="25" Margin="402,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="115" TextChanged="TextBox_TextChanged"/>
  10. <TextBlock HorizontalAlignment="Left" Height="20" Margin="336,8,0,0" TextWrapping="Wrap" Text="筛选内容" VerticalAlignment="Top" Width="61"/>
  11. </Grid>
  1. public MainWindow()
  2. {
  3. InitializeComponent();
  4. Preson = new ObservableCollection<Preson>() {
  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> Preson
  18. {
  19. get { return (ObservableCollection<Preson>)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<Preson>), 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 数据源排序


下一篇:c# – 将非属性绑定到datagrid列DataField?