排序在WPF数据网格的多个列 [英] Sort on multiple columns in WPF datagrid

查看:226
本文介绍了排序在WPF数据网格的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何设置我的WPF的DataGrid进行排序相似,有两个可排序的列多列,点击第一列的标题为主要排序,然后按住Shift点击第二列的标题为辅助排序。我想,当用户在第一列的标题点击,而无需shift点击第二列标题的多列排序自动发生。有没有办法在XAML完全做到这一点?如果没有我怎样才能在代码中做到这一点的背后?目前使用VB.Net,但一个C#代码段是可以接受的,如果你有一个。 !谢谢

How can I set up my WPF datagrid to sort on multiple columns similar to having two sortable columns, clicking on the header of the first column for a primary sort and then SHIFT clicking on the header of the second column for a secondary sort. I would like the multiple column sort to happen automatically when the user clicks on the header of the first column without having to SHIFT click on the second column header. Is there a way to do this entirely in the xaml? If not how can I do this in the code behind? Currently using VB.Net but a C# snippet is acceptable if you have one. Thanks!

推荐答案

您可以通过添加 System.ComponentModel 命名空间是这样做的:

You can do this by adding System.ComponentModel namespace like this:

xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"

然后在 Col​​lectionViewSource XAML添加新的 SortDescriptions 是这样的:

then inside the CollectionViewSource XAML add new SortDescriptions like this:

<CollectionViewSource … >
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Column1"/>
                <scm:SortDescription PropertyName="Column2"/>
            </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

这将排序列1,列2数据网格。

this will sort datagrid on column1,column2.

编辑:

也这样使用C#代码背后,是很容易的:

also doing this using C# code behind is pretty easy :

    private void btnSort_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("The_ViewSource_Name")));
        myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending));
        myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending));
    }



EDIT2:

解决方法,可向赶上列标题点击鼠标左键事件,并防止排序网格上像这样的列:

Workaround can be made to catch the column header left mouse click event and prevent the grid from sort on that column like this:


  • 禁用网格属性命名
    CanUserSortColumns

  • Disable grid property named CanUserSortColumns


  • 添加这段代码到网格
    的PreviewMouseLeftButtonUp事件:

  • Add this code to the grid PreviewMouseLeftButtonUp event :

private void myDataGrid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    DependencyObject dep = (DependencyObject)e.OriginalSource;
    while ((dep != null) &&
    !(dep is DataGridCell) &&
    !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return;

    if (dep is DataGridColumnHeader)
    {
        DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
        // check if this is the wanted column
        if (columnHeader.Column.Header.ToString() == "The_Wanted_Column_Title")
        {
            System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource")));
            myViewSource.SortDescriptions.Clear();
            myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending));
            myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending));
        }
        else
        {
            //usort the grid on clicking on any other columns, or maybe do another sort combination
            System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource")));
            myViewSource.SortDescriptions.Clear();
        }

    }
}


您可以修改和扩展这个代码来实现您的要求。

You can modify and expand this code to achieve your requirements.

这篇关于排序在WPF数据网格的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆