wpf 具有自定义排序的 CollectionViewSource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24147585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:53:29  来源:igfitidea点击:

CollectionViewSource with custom sort

wpfvb.netsortingcollectionviewsource

提问by SimCity

I'm new to WPF and I'm having difficulty trying to sort a CollectionViewSource with a custom sort. Here's the situation:

我是 WPF 的新手,我在尝试使用自定义排序对 CollectionViewSource 进行排序时遇到了困难。这是情况:

I have a SearchView that is called with a parameter which becomes it's datacontext like so:

我有一个 SearchView,它被一个参数调用,它变成了它的数据上下文,如下所示:

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

In the SearchView.xaml, I then bind the CollectionViewSource to the collection :

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

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

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?

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

回答by

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 ListCollectionViewusing the Viewproperty of the CollectionViewSource.

为了对 使用自定义排序器CollectionViewSource,您必须等到ItemsControl(例如列表框)加载;然后你可以得到ListCollectionViewusing 的View属性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:

作为说明,这里有一个小示例,它以两种不同的方式显示整数列表:上方的列表框应用自定义排序顺序,而下方的列表框未排序:

screen shot

截屏

MainWindow.xaml:

主窗口.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:

主窗口.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;
        }
    }
}