wpf – 具有自定义排序的CollectionViewSource

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

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

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

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

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

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

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

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

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:

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;
        }
    }
}
原文链接:https://www.f2er.com/vb/255187.html

猜你在找的VB相关文章