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. (译)【Unity教程】使用Unity开发Windows Phone上的横版跑酷游戏

    译者注: 目前移动设备的跨平台游戏开发引擎基本都是采用Cocos2d-x或者Unity.一般而言2d用cocos2d-x 3d用unity,但是对于Windows Phone开发者, cocos2d- ...

  2. linux中socket的理解

    对linux中socket的理解 一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打开open –> 读写write/read –> ...

  3. android:imeOptions属性(转)

    默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘 设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光 ...

  4. Android三种基本的加载网络图片方式(转)

    Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...

  5. ubuntu中禁用华硕S550C触摸屏的方法

    华硕S550C的触摸屏被我一不小心弄了一条裂缝,导致屏幕一直会莫名其妙自动进行点击,严重影响了使用.在windows 系统下通过FN+F7的快捷键可以直接禁用触摸屏,但是换成ubuntu 系统之后,快 ...

  6. 二维数组&多维数组

    1.二维数组 二维数组由多个一维数组组成,其定义方式: ,]{ {,,,}, {,,,}, {,,,} }; 二维数组中括号中,逗号左边表示一维数组的个数,也可以说控制行,逗号后面的数表示每个一维数组 ...

  7. C#开发微信公众平台-就这么简单(附Demo)(转)

    原文:http://www.cnblogs.com/xishuai/p/3625859.html 写在前面 阅读目录: 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文. ...

  8. JavaScript中设置元素class的三种方法小结

    第一.element.setAttribute('class','abc');  第二.element.setAttribute('className', 'abc') : 第三.element.cl ...

  9. Android学习系列(39)--Android主题和样式之系统篇(上)

    [基于最新的Android4.4的源码分析] 每家公司或者每个移动团队无不想开发出一套自己的UI框架,融入自己的设计和特性,这必然会去修改android的ui.所以,学习和理解android的UI设计 ...

  10. 彻底搞懂Html5本地存储技术(一)

    一.H5之前客户端本地存储的实现 1. cookies cookies的应用比较广泛,但有以下几个问题: (1)每次http请求头上会带着,浪费资源 (2)每个域名客户端只能存储4K大小 (3)会造成 ...