WPF ListView排序在列上单击 [英] WPF ListView sorting on column click

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

问题描述

我有一个列表视图,该列表视图在运行时以具有单列/多列的网格的形式添加项.现在,我需要进行排序.列表视图中包含项目后,如果他们单击该列,则应在该列上对其进行排序.

I have a listview for which the items gets added at run time in form of a grid with single/multiple columns. Now I need to get the sort working. Once the list view has items in it and they click on the column it should sort it on that column.

下面是列表视图的代码

<ListView Name="lstValue" Margin="0,0,0,10"></ListView>

用于填充列表视图的C#代码:

C# code where it populates the list view:

 case "Person":
                        dt = GetDataTable(GET_Person)
                        this.lstValue.View = gridview;
                        gridview.Columns.Add(new GridViewColumn { Header = "Number", 
                            DisplayMemberBinding = new Binding("Number") });
                        gridview.Columns.Add(new GridViewColumn { Header = "Name", 
                            DisplayMemberBinding = new Binding("Name") });
                        foreach(DataRow dr in dt.Rows)
                        {
                                                          this.lstValue.Items.Add(new ReportItem { Number = dr["Number"].ToString(),
                                Name = dr["Name"].ToString() });
                        }
                        break;

他们应该能够对姓名或号码进行排序.

They should be able to sort on name or number.

推荐答案

链接是MSDN方式.最主要的是处理对gridview列标题的单击.

This link is the MSDN way. The main thing is to handle the click on the gridview column header.

<ListView x:Name='lv' Height="150" HorizontalAlignment="Center" 
  VerticalAlignment="Center" 
  GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler"
 >

在代码中:

GridViewColumnHeader _lastHeaderClicked = null;
ListSortDirection _lastDirection = ListSortDirection.Ascending;

void GridViewColumnHeaderClickedHandler(object sender,RoutedEventArgs e)
{
  GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
  ListSortDirection direction;

  if (headerClicked != null)
  {
      if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
      {
          if (headerClicked != _lastHeaderClicked)
          {
             direction = ListSortDirection.Ascending;
          }
          else
          {
             if (_lastDirection == ListSortDirection.Ascending)
             {
               direction = ListSortDirection.Descending;
             }
             else
             {
                 direction = ListSortDirection.Ascending;
             }
          }

          string header = headerClicked.Column.Header as string;
          Sort(header, direction);

          _lastHeaderClicked = headerClicked;
          _lastDirection = direction;
       }
    }
  }

 private void Sort(string sortBy, ListSortDirection direction)
 {
  ICollectionView dataView =
    CollectionViewSource.GetDefaultView(lv.ItemsSource);

  dataView.SortDescriptions.Clear();
  SortDescription sd = new SortDescription(sortBy, direction);
  dataView.SortDescriptions.Add(sd);
  dataView.Refresh();

}

基本上就是这样.我没有在列标题上添加任何方向字形来显示方向.如果要查看操作方法,可以参考完整的教程(请参见上面的链接).

Basically that's it. I did not include adding little direction glyphs on the column header to show the direction. If you want to see how to do that you can refer to the full tutorial (see link above).

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

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