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

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

问题描述

如何设置我的WPF数据网格来排序多列,类似于有两个可排序的列,点击第一列的标题进行主排序,然后单击第二列的标题进行辅助排序。当用户单击第一列的标题时,不需要SHIFT单击第二列标题,我希望多列排序会自动发生。有没有办法完全在xaml这样做?如果不是我该如何在代码背后呢?目前使用VB.Net但C#代码片段可以接受,如果你有一个。谢谢!

解决方案

您可以通过添加 System.ComponentModel 命名空间来执行此操作:

  xmlns:scm =clr-namespace:System.ComponentModel; assembly = WindowsBase
/ pre>

然后在 CollectionViewSource 中,XAML添加新的 SortDescriptions ,如下所示:

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

这将在column1,column2上排序datagrid。



编辑:



也使用C#代码执行此操作很简单:

  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:



可以解决方法来捕获列标题鼠标左键事件,并阻止网格在该列上排序,如下所示:




  • 禁用名为
    的网格属性CanUserSortColumns






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

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

    if(dep == null)
    return;

    如果(dep是DataGridColumnHeader)
    {
    DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
    //检查这是否是需要的列
    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
    {
    //在任何其他列上点击网格,或者可以执行其他排序组合
    System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource(myViewSource)));
    myViewSource.SortDescriptions.Clear();
    }

    }
    }




您可以修改和扩展此代码以达到您的要求。


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!

解决方案

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

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

then inside the CollectionViewSource XAML add new SortDescriptions like this:

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

this will sort datagrid on column1,column2.

Edit:

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:

  • Disable grid property named CanUserSortColumns

  • 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天全站免登陆