具有自定义排序的CollectionViewSource [英] CollectionViewSource with custom sort
问题描述
我是WPF的新手,我很难尝试使用自定义排序对CollectionViewSource进行排序.情况如下:
I'm new to WPF and I'm having difficulty trying to sort a CollectionViewSource with a custom sort. Here's the situation:
我有一个带有参数的SearchView,它变成了这样的数据上下文:
I have a SearchView that is called with a parameter which becomes it's datacontext like so:
mainView.SetGlobalOverlay(New SearchView With {.DataContext = interventionViewModel})
然后在SearchView.xaml中,将CollectionViewSource绑定到集合:
In the SearchView.xaml, I then bind the CollectionViewSource to the collection :
<CollectionViewSource x:Key="UnitsCollection"
Filter="UnitsCollection_Filter"
Source="{Binding Path=Units}" />
现在,我已经在另一个共享类中实现了IComparer接口.此比较器可在源代码中其他位置的ListCollectionView上使用,并且可以正常工作.现在,如何在CollectionViewSource上重用此比较器?
Now, I already have an IComparer interface implemented in another shared class. This comparer is used on a ListCollectionView somewhere else in the sourcecode and works fine. Now, how can I re-use this comparer on a CollectionViewSource?
推荐答案
为了对CollectionViewSource
使用自定义分类器,您必须等到ItemsControl
(例如列表框)被加载;那么您可以使用CollectionViewSource
的View
属性获取ListCollectionView
.
In order to use the custom sorter for the CollectionViewSource
, you have to wait until the ItemsControl
(e.g. a list box) is loaded; then you can get the ListCollectionView
using the View
property of the CollectionViewSource
.
作为示例,下面是一个小示例,它以两种不同的方式显示整数列表:上方的列表框应用自定义排序顺序,而下方的列表框未排序:
As illustration, here is a small example that displays a list of integers in two different ways: the upper list box applies a custom sort order, whereas the lower list box is unsorted:
MainWindow.xaml:
MainWindow.xaml:
<Window x:Class="WpfApplication27.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="300">
<Window.Resources>
<CollectionViewSource x:Key="MyCollectionViewSource1" Source="{Binding RawData}" />
<CollectionViewSource x:Key="MyCollectionViewSource2" Source="{Binding RawData}" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListBox Grid.Row="0" Margin="5" Background="LightSkyBlue"
ItemsSource="{Binding Source={StaticResource MyCollectionViewSource1}}"/>
<ListBox Grid.Row="1" Margin="5" Background="LightYellow"
ItemsSource="{Binding Source={StaticResource MyCollectionViewSource2}}"/>
</Grid>
</Window>
MainWindow.xaml.cs:
MainWindow.xaml.cs:
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication27
{
public partial class MainWindow : Window
{
public ObservableCollection<int> RawData { get; private set; }
public MainWindow()
{
RawData = new ObservableCollection<int> { 10, 222, 1, 333, 2, 777, 6 };
InitializeComponent();
DataContext = this;
this.Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
CollectionViewSource source = (CollectionViewSource)(this.Resources["MyCollectionViewSource1"]);
ListCollectionView view = (ListCollectionView)source.View;
view.CustomSort = new CustomSorter();
}
}
// Sort by number of digits (descending), then by value (ascending)
public class CustomSorter : IComparer
{
public int Compare(object x, object y)
{
int digitsX = x.ToString().Length;
int digitsY = y.ToString().Length;
if (digitsX < digitsY)
{
return 1;
}
else if (digitsX > digitsY)
{
return -1;
}
return (int) x - (int) y;
}
}
}
这篇关于具有自定义排序的CollectionViewSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!