Sorting outside the collection

    protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (Settings.AscendingSort.Value)
{
App.PictureList.Pictures = new ObservableCollection<Models.Picture>(App.PictureList.Pictures.OrderBy(x => x.DateTaken)) as System.Collections.IList;
Recent.ItemsSource = App.PictureList.Pictures;
}
else
{
App.PictureList.Pictures = new ObservableCollection<Models.Picture>(App.PictureList.Pictures.OrderByDescending(x => x.DateTaken)) as System.Collections.IList;
Recent.ItemsSource = App.PictureList.Pictures;
}
  }

Sort on the XAML View

http://msdn.microsoft.com/en-us/library/ms742542.aspx

// You can sort the view of the collection rather that sorting the collection itself

// xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
<myView.Resources>
<CollectionViewSource x:Key="ItemListViewSource" Source="{Binding Itemlist}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="{Binding SortingProperty}" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</myView.Resources>
And then you can use the CollectionViewSource as ItemSource: ItemsSource="{Binding Source={StaticResource ItemListViewSource}}"

Sorted Observable Collection

namespace SortedCollection
{
/// <summary>
/// SortedCollection which implements INotifyCollectionChanged interface and so can be used
/// in WPF applications as the source of the binding.
/// </summary>
/// <author>consept</author>
public class SortedObservableCollection<TValue> : SortedCollection<TValue>, INotifyPropertyChanged, INotifyCollectionChanged
{
public SortedObservableCollection() : base() { } public SortedObservableCollection(IComparer<TValue> comparer) : base(comparer) { } // Events
public event NotifyCollectionChangedEventHandler CollectionChanged; public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (this.CollectionChanged != null)
{
this.CollectionChanged(this, e);
}
} private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index)
{
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index));
} private void OnCollectionChanged(NotifyCollectionChangedAction action, object oldItem, object newItem, int index)
{
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index));
} private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index, int oldIndex)
{
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index, oldIndex));
} private void OnCollectionReset()
{
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
} protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, e);
}
} private void OnPropertyChanged(string propertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
} public override void Insert(int index, TValue value)
{
base.Insert(index, value);
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Add, value, index);
} public override void RemoveAt(int index)
{
var item = this[index];
base.RemoveAt(index);
this.OnPropertyChanged("Item[]");
this.OnPropertyChanged("Count");
this.OnCollectionChanged(NotifyCollectionChangedAction.Remove, item, index);
} public override TValue this[int index]
{
get
{
return base[index];
}
set
{
var oldItem = base[index];
base[index] = value;
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Replace, oldItem, value, index);
}
} public override void Clear()
{
base.Clear();
OnCollectionReset();
}
}
}

Example

/*
* samples:
* //sort ascending
* MySortableList.Sort(x => x.Name, SortDirection.Ascending);
*
* //sort descending
* MySortableList.Sort(x => x.Name, SortDirection.Descending);
*/ public enum SortDirection
{
Ascending,
Descending
} public class SortableObservableCollection : ObservableCollection
{
#region Consts, Fields, Events #endregion #region Methods public void Sort(Func keySelector, SortDirection direction)
{
switch (direction)
{
case SortDirection.Ascending:
{
applySort(Items.OrderBy(keySelector));
break;
}
case SortDirection.Descending:
{
applySort(Items.OrderByDescending(keySelector));
break;
}
}
} public void Sort(Func keySelector, IComparer comparer)
{
applySort(Items.OrderBy(keySelector, comparer));
} private void applySort(IEnumerable sortedItems)
{
var sortedItemsList = sortedItems.ToList(); foreach (var item in sortedItemsList)
{
Move(IndexOf(item), sortedItemsList.IndexOf(item));
}
} #endregion
} ///
/// Provides automatic sorting, when items are added/removed
///
///
public class SortedObservableCollection : SortableObservableCollection
{ #region Consts, Fields, Events private readonly IComparer _comparer; #endregion #region Methods public SortedObservableCollection(IComparer comparer)
{
Condition.Requires(comparer, “
comparer”).
IsNotNull();
_comparer = comparer;
} protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
Sort();
} protected override void RemoveItem(int index)
{
base.RemoveItem(index);
Sort();
} public void Sort()
{
Sort(item => item, _comparer);
} #endregion
} ///
/// Whenever a property of the item changed, a sorting will be issued.
///
///
public class SortedObservableCollectionEx : SortedObservableCollection where T : class, INotifyPropertyChanged
{
#region Consts, Fields, Events #endregion #region Methods public SortedObservableCollectionEx(IComparer comparer)
: base(comparer)
{
} protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
if (item != null)
{
item.PropertyChanged += handleItemPropertyChanged;
}
} protected override void RemoveItem(int index)
{
T item = this[index];
if (item != null)
{
item.PropertyChanged -= handleItemPropertyChanged;
} base.RemoveItem(index);
} private void handleItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
Sort();
} #endregion }

  

Sortable Observable Collection in C#的更多相关文章

  1. Reactive Extensions介绍

    Reactive Extensions(Rx)是对LINQ的一种扩展,他的目标是对异步的集合进行操作,也就是说,集合中的元素是异步填充的,比如说从Web或者云端获取数据然后对集合进行填充.Rx起源于M ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (24) ------ 第五章 加载实体和导航属性之查询内存对象

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-4  查询内存对象 问题 你想使用模型中的实体对象,如果他们已经加载到上下文中, ...

  3. A Xamarin.Forms Infinite Scrolling ListView

    from:http://www.codenutz.com/lac09-xamarin-forms-infinite-scrolling-listview/ The last few months ha ...

  4. Data Binding(数据绑定)用户指南

    1)介绍 这篇文章介绍了如何使用Data Binding库来写声明的layouts文件,并且用最少的代码来绑定你的app逻辑和layouts文件. Data Binding库不仅灵活而且广泛兼容- 它 ...

  5. Reactive ExtensionsLINQ和Rx简单介绍

    LINQ和Rx简单介绍 相信大家都用过Language Integrated Query (LINQ),他是一种强大的工具能够从集合中提取数据.Reactive Extensions(Rx)是对LIN ...

  6. Reactive Extensions

    Rx提供了一种新的组织和协调异步事件的方式,极大的简化了代码的编写.Rx最显著的特性是使用可观察集合(Observable Collection)来达到集成异步(composing asynchron ...

  7. How to Add Columns to a DataGrid through Binding and Map Its Cell Values

    How to Add Columns to a DataGrid through Binding and Map Its Cell Values Lance Contreras, 7 Nov 2013 ...

  8. lightswitch 添加 TreeView 控件

    代码片段 <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&q ...

  9. (MVVM) ListBox Binding 和 实时刷新

    当需要用Lisbbox 来log 一些记录的时候,ObservableCollection 并不可以是记录实时的反应在WPF 的UI上面. 这个时候就需要用一个异步collection 来完成. // ...

随机推荐

  1. mysql_multi启动数据库

    1.初始化数据库 在$mysql_base目录下,新增加存放data的文件夹,用mysql_install_db命令执行初始化 [root@ora11g scripts]# ./mysql_insta ...

  2. poj 3468【线段树】

    题意:给定Q(1<=Q<=100000)个数A1,A2…AQ,以及可能多次进行的两个操作 1)对某个区间Ai……Aj的每个数都加n(n可变) 2)对某个区间Ai……Aj的数求和 分析: 树 ...

  3. JMockit

    [TOC] 简介 JMockit是基于JavaSE5中的java.lang.instrument包开发,内部使用ASM库来动态修改java的字节码,使得java这种静态语言可以想动态脚本语言一样动态设 ...

  4. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  5. hdu 4281(MTSP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4281 题意:给出N个点,第一个点是裁判,其他N-1个点需要裁判过去回答问题,每个点需要的时间不一样,而 ...

  6. 对于一个负数mod正数

    鸟神说.. a/b靠零取整 然后呢..a%b定义成a-(a/b)*b c语言就是这么算的... 那么python2.6是怎么算的呢 如果最后你取模想得到一个正数.. 那么在上述取模定义不变的情况下 p ...

  7. 对android录制的NV21视频数据进行旋转(90,180,270)与剪切

    android默认的视频采集格式是NV21,(属于YUV420) 在onPreviewFrame中传进来的byte[] data即为NV21格式. 旋转算法 对NV21进行顺时针旋转90度,180度和 ...

  8. position之absolute与relative 详解

    absolute:绝对定位: relative:相对定位: 唉,以前只是知是知道这两个单词的汉语意思,然后呢,,,怎么用...也是摸凌两可的用.终于抽出时间来看看了: 1.绝对定位:absulute ...

  9. hdu1863 最小生成树(prim)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1863 思路:最小生成树模板题,直接套模板. 代码: #include<iostrea ...

  10. css3 -- 属性选择器

    属性选择器: 1.CSS属性选择器 属性选择器E[attr="value"]{} 包含属性选择器E[attr~="value"]{} 2.CSS3的新属性选择器 ...