场景:当点击ListView列标题,对该列排序
今天有位朋友问我,怎么实现对ListView的列排序,我是好人,自然要帮忙下了,OK,用代码说话
XAML
<Window x:Class="ListViewSort.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<ListView x:Name="lv" GridViewColumnHeader.Click="GridViewColumnHeader_Click" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Margin="0,0,0,70">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
<Window x:Class="ListViewSort.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<ListView x:Name="lv" GridViewColumnHeader.Click="GridViewColumnHeader_Click" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Margin="0,0,0,70">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
namespace ListViewSort
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Initialize datasource
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(5, "Apple");
dt.Rows.Add(17, "Orange");
dt.Rows.Add(3, "Banana");
lv.DataContext = dt;
}
private void GridViewColumnHeader_Click(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is GridViewColumnHeader)
{
//Get clicked column
GridViewColumn clickedColumn = (e.OriginalSource as GridViewColumnHeader).Column;
if (clickedColumn != null)
{
//Get binding property of clicked column
string bindingProperty = (clickedColumn.DisplayMemberBinding as Binding).Path.Path;
SortDescriptionCollection sdc = lv.Items.SortDescriptions;
ListSortDirection sortDirection = ListSortDirection.Ascending;
if (sdc.Count > 0)
{
SortDescription sd = sdc[0];
sortDirection = (ListSortDirection)((((int)sd.Direction) + 1) % 2);
sdc.Clear();
}
sdc.Add(new SortDescription(bindingProperty, sortDirection));
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
namespace ListViewSort
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Initialize datasource
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(5, "Apple");
dt.Rows.Add(17, "Orange");
dt.Rows.Add(3, "Banana");
lv.DataContext = dt;
}
private void GridViewColumnHeader_Click(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is GridViewColumnHeader)
{
//Get clicked column
GridViewColumn clickedColumn = (e.OriginalSource as GridViewColumnHeader).Column;
if (clickedColumn != null)
{
//Get binding property of clicked column
string bindingProperty = (clickedColumn.DisplayMemberBinding as Binding).Path.Path;
SortDescriptionCollection sdc = lv.Items.SortDescriptions;
ListSortDirection sortDirection = ListSortDirection.Ascending;
if (sdc.Count > 0)
{
SortDescription sd = sdc[0];
sortDirection = (ListSortDirection)((((int)sd.Direction) + 1) % 2);
sdc.Clear();
}
sdc.Add(new SortDescription(bindingProperty, sortDirection));
}
}
}
}
}
此Demo的前提是ListView使用绑定。
排序是根据列的类型来的,就ID列来说,int类型的排序结果是3,5,17,而如果你把该列
类型改为string,结果就会是17,3,5,如果你定义列的时候不加类型,默认是string,如
果是自定义类型,那么请继承IComparable接口,实现CompareTo方法。
源码下载
谨以此记录成长的脚步,同时和大家一起分享快乐。