排序是根据列的类型来的,就ID列来说,int类型的排序结果是3,5,17,而如果你把该列类型改为string,结果就会是17,3,5,如果你定义列的时候不加类型,默认是string,如果是自定义类型,那么请继承IComparable接口,实现CompareTo方法。

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>

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));
}
}
}
}
}

实现单击列表头对ListView的动态排序的更多相关文章

  1. ActiveReports 报表应用教程 (9)---交互式报表之动态排序

    在 ActiveReports 中除了提供对数据源进行排序的功能之外,还提供了最终用户排序功能,最终用户可以对报表进行区域内排序和整个数据源排序,结合数据钻取.过滤等功能可以让用户更方便地分析报表数据 ...

  2. C# listview 单击列头实现排序 <二>

    单击列头实现排序,首先在羡慕中添加下面的帮助实现的类:具体的代码: using System; using System.Collections; using System.Windows.Forms ...

  3. element ui的表格列设置fixed后做动态表格出现表格错乱

    最近使用element-UI时,使用table做动态表格,当操作列使用fixed时,动态切换表格列设置设置时就会出现错乱,情况如下: 解决方法: 把el-table-column上的key设成一个随机 ...

  4. Android零基础入门第44节:ListView数据动态更新

    原文:Android零基础入门第44节:ListView数据动态更新 经过前面几期的学习,关于ListView的一些基本用法大概学的差不多了,但是你可能发现了,所有ListView里面要填充的数据都是 ...

  5. 在论坛中出现的比较难的sql问题:6(动态行转列 考试科目、排名动态列问题)

    原文:在论坛中出现的比较难的sql问题:6(动态行转列 考试科目.排名动态列问题) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 下面的几个问题,都是动态行转列的问题. ...

  6. LinQ动态排序

    LinQ动态排序 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为sort这个名称填写错误,会导致后台一直无法获取datagrid的排序字段 本来是没有这一讲的,为了使2 ...

  7. MySQL 按照数据库表字段动态排序 查询列表信息

    MySQL 按照数据库表字段动态排序 查询列表信息 背景描述 项目中数据列表分页展示的时候,前端使用的Table组件,每列自带对当前页的数据进行升序或者降序的排序. 但是客户期望:随机点击某一列的时候 ...

  8. 40行Python制作超炫酷动态排序图,有了它高逼格PPT再也不愁!

        本文首发于量化投资与机器学习 转载于  https://mp.weixin.qq.com/s/KaB_7oXZf0_IV97y0pRPmQ 前言 最近,这种动态排序条形图视频超级火,如下图: ...

  9. linq扩展之动态排序

    前两天看QQ群里面,一位朋友问的问题,说在linq中怎么实现动态排序呢,自己想了半天,没有头绪,网上找了下相关的资料,看了下,收益挺多,记录下来. 之前我们没有如果不知道动态排序的方法的话,我们可能会 ...

随机推荐

  1. vue.js源码学习分享(九)

    /* */ var arrayKeys = Object.getOwnPropertyNames(arrayMethods);//获取arrayMethods的属性名称 /** * By defaul ...

  2. Z-Order(转)

    原文转自 http://www.th7.cn/system/win/201406/60715.shtml 窗口在子窗口链中的先后顺序也就是窗口在屏幕上显示时的前后顺序,在子窗口链里位置越靠前的窗口显示 ...

  3. 【Visual Studio】error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800” (转)

    1.案例一 _MSC_VER 定义编译器的版本.下面是一些编译器版本的_MSC_VER值:MS VC++ 10.0 _MSC_VER = 1600MS VC++ 9.0 _MSC_VER = 1500 ...

  4. Chrome 浏览器如何完美实现滚动截图技巧

    一.前言 我们平时在浏览网页时,想把碰到好的网页内容或者文章截屏保存,但是网页的长度常常会超出屏幕高度,一般的截屏功能只能截取显示在屏幕上的内容,那我们该如何方便快捷截取全部内容?今天就分享一个如何利 ...

  5. (1)hello world

    操作系统安装SDK     https://www.microsoft.com/net/download/windows 选择对应的操作系统 wget -q https://packages.micr ...

  6. cmake ccmake

    下载libqrencode源码编译过程 git clone https://github.com/fukuchi/libqrencode.git 2001  mkdir build 2002  cd ...

  7. Remove Nth Node From End of List(链表,带测试代码)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. 小W摆石子

    可以确定, 最后围成是 一个长方形 + 多出一列 的形状. 而且多出的那一列应该是和较短的边相邻. 贴代码. #include<iostream> #include<algorith ...

  9. 几点iOS开发技巧

    转自I'm Allen的博客   原文:iOS Programming Architecture and Design Guidelines   原文来自破船的分享   原文作者是开发界中知晓度相当高 ...

  10. 老毛桃winpe优盘启动系统个性修改全攻略

    PE优盘系统也有很多:大白菜.老毛桃.深度.通用PE工具箱.U大师.电脑店……这些PE优盘系统大多都会捆绑软件安装.更改主页等,一不小心,你就中招.虽然有些是可以自己去取消,但是启动画面还是带有各种L ...