wpf – 具有自定义排序的CollectionViewSource

前端之家收集整理的这篇文章主要介绍了wpf – 具有自定义排序的CollectionViewSource前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 WPF的新手,我在尝试使用自定义排序对CollectionViewSource进行排序时遇到了困难.情况如下:

我有一个使用参数调用的SearchView,它变成了它的datacontext,如下所示:

  1. mainView.SetGlobalOverlay(New SearchView With {.DataContext = interventionviewmodel})

在SearchView.xaml中,然后我将CollectionViewSource绑定到集合:

  1. <CollectionViewSource x:Key="UnitsCollection"
  2. Filter="UnitsCollection_Filter"
  3. Source="{Binding Path=Units}" />

现在,我已经在另一个共享类中实现了IComparer接口.此比较器用于源代码中其他位置的ListCollectionView,并且工作正常.现在,我如何在CollectionViewSource上重用这个比较器?

为了使用CollectionViewSource的自定义排序器,您必须等到ItemsControl(例如列表框)加载;然后,您可以使用CollectionViewSource的View属性获取ListCollectionView.

如图所示,这是一个小例子,它以两种不同的方式显示整数列表:上面的列表框应用自定义排序顺序,而下面的列表框是未排序的:

MainWindow.xaml:

  1. <Window x:Class="WpfApplication27.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:clr="clr-namespace:System;assembly=mscorlib"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. Title="MainWindow" Height="350" Width="300">
  6. <Window.Resources>
  7. <CollectionViewSource x:Key="MyCollectionViewSource1" Source="{Binding RawData}" />
  8. <CollectionViewSource x:Key="MyCollectionViewSource2" Source="{Binding RawData}" />
  9. </Window.Resources>
  10. <Grid>
  11. <Grid.RowDefinitions>
  12. <RowDefinition/>
  13. <RowDefinition/>
  14. </Grid.RowDefinitions>
  15. <ListBox Grid.Row="0" Margin="5" Background="LightSkyBlue"
  16. ItemsSource="{Binding Source={StaticResource MyCollectionViewSource1}}"/>
  17. <ListBox Grid.Row="1" Margin="5" Background="LightYellow"
  18. ItemsSource="{Binding Source={StaticResource MyCollectionViewSource2}}"/>
  19. </Grid>
  20. </Window>

MainWindow.xaml.cs:

  1. using System.Collections;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using System.Windows.Data;
  5.  
  6. namespace WpfApplication27
  7. {
  8. public partial class MainWindow : Window
  9. {
  10. public ObservableCollection<int> RawData { get; private set; }
  11.  
  12. public MainWindow()
  13. {
  14. RawData = new ObservableCollection<int> { 10,222,1,333,2,777,6 };
  15.  
  16. InitializeComponent();
  17.  
  18. DataContext = this;
  19.  
  20. this.Loaded += MainWindow_Loaded;
  21. }
  22.  
  23. void MainWindow_Loaded(object sender,RoutedEventArgs e)
  24. {
  25. CollectionViewSource source = (CollectionViewSource)(this.Resources["MyCollectionViewSource1"]);
  26. ListCollectionView view = (ListCollectionView)source.View;
  27. view.CustomSort = new CustomSorter();
  28. }
  29. }
  30.  
  31. // Sort by number of digits (descending),then by value (ascending)
  32. public class CustomSorter : IComparer
  33. {
  34. public int Compare(object x,object y)
  35. {
  36. int digitsX = x.ToString().Length;
  37. int digitsY = y.ToString().Length;
  38. if (digitsX < digitsY)
  39. {
  40. return 1;
  41. }
  42. else if (digitsX > digitsY)
  43. {
  44. return -1;
  45. }
  46. return (int) x - (int) y;
  47. }
  48. }
  49. }

猜你在找的VB相关文章