如何对ObservableCollection进行排序 [英] how to sort ObservableCollection

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

问题描述

我有一个ObservableCollection,并且WPF UserControl已数据绑定到它.控件是一个图形,显示了ObservableCollection中BarData类型的每个项目的垂直条.

I have a an ObservableCollection and a WPF UserControl is Databound to it. The Control is a graph that shows a vertical bar for each item of type BarData in the ObservableCollection.

ObservableCollection<BarData>

class BarData
{
   public DateTime StartDate {get; set;}
   public double MoneySpent {get; set;}
   public double TotalMoneySpentTillThisBar {get; set;}
}

现在,我想基于StartDate整理ObservableCollection,以便BarData在集合中按StartDate的升序排列. 然后,我可以像这样在每个BarData中计算TotalMoneySpentTillThisBar的值-

Now I want to sort out the ObservableCollection based on StartDate so that the BarData's will be in increasing order of StartDate in the collection. Then I can calculate values of TotalMoneySpentTillThisBar in each BarData like this -

var collection = new ObservableCollection<BarData>();
//add few BarData objects to collection
collection.Sort(bar => bar.StartData);    // this is ideally the kind of function I was looking for which does not exist 
double total = 0.0;
collection.ToList().ForEach(bar => {
                                     bar.TotalMoneySpentTillThisBar = total + bar.MoneySpent;
                                     total = bar.TotalMoneySpentTillThisBar; 
                                   }
                            );

我知道我可以使用ICollectionView进行排序,筛选数据以进行筛选,但这不会更改实际的集合.我需要对实际集合进行排序,以便可以为每个项目计算TotalMoneySpentTillThisBar.它的值取决于集合中项目的顺序.

I know I can use ICollectionView to sort, filter data for veiwing but that does not change the actual collection. I need to sort the actual collection so that I can calculate TotalMoneySpentTillThisBar for each item. Its value depends on order of items in colection.

谢谢.

推荐答案

哼哼,我对您的第一个问题是: 对ObservableCollection进行排序真的很重要,还是对GUI中的显示进行排序是您真正想要的?

hummm first question I have for you is: is it really important that your ObservableCollection is sorted, or is what you really want is to have the display in GUI sorted?

我认为目标是要有一个分类的显示,它将实时"更新.然后我看到了两种解决方案

I assume that the aim is to have a sorted display that will be updated "real time". Then I see 2 solutions

  1. 获取ObservableCollectionICollectionView并对其进行排序,如此处所述 http://marlongrech.wordpress.com/2008/11/22/icollectionview-说明/

  1. get the ICollectionView of your ObservableCollection and sort it, as explained here http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/

将您的ObservableCollection绑定到CollectionViewsource,对其进行排序,然后将该CollectionViewSource用作ListViewItemSource.

bind your ObservableCollection to a CollectionViewsource, add a sort on it, then use thatCollectionViewSource as the ItemSource of a ListView.

即:

添加此命名空间

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

然后

<CollectionViewSource x:Key='src' Source="{Binding MyObservableCollection, ElementName=MainWindowName}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="MyField" />
    </CollectionViewSource.SortDescriptions>

</CollectionViewSource>

并这样绑定

<ListView ItemsSource="{Binding Source={StaticResource src}}" >

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

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