WPF:对列表框进行排序 [英] WPF: Sort a list box

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

问题描述

如何按两个字段对ListBox进行排序? (在这种情况下,我的模型类的ApplicationNameInstanceName属性.)

How do I sort a ListBox by two fields? (In this case the ApplicationName and InstanceName properties of my model class.)

推荐答案

这取决于您的数据源.这有几种方法....

It depends on your data source. Here are a couple of ways....

在lisbox数据源上使用linq

using linq on lisbox data source

来自LINQ样本101 :

string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var sortedDigits =
        from d in digits
        orderby d.Length, d
        select d;

使用CollectionView作为列表框,并添加 SortDescription

use a CollectionView for your listbox and add a SortDescription

ICollectionView myDataView = CollectionViewSource.GetDefaultView(myData);

using (myDataView.DeferRefresh()) // we use the DeferRefresh so that we refresh only once
{
   myDataView.SortDescriptions.Clear();
   if (SortById)
      myDataView.SortDescriptions.Add(new SortDescription("ApplicationName", direction));
   if (SortByName)
         myDataView.SortDescriptions.Add(new SortDescription("InstanceName", direction));
}

这篇关于WPF:对列表框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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