ToObservableCollection<T>()

Convert a IEnumerable to a ObservableCollection and can be used in XAML (Silverlight and WPF) projects

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Databinding01 {
public static class CollectionExtensions {

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> coll) {
var c = new ObservableCollection<T>();
foreach (var e in coll)
c.Add(e);
return c;
}
}
}

示例:

1
2
3
4
5
var list = new ObservableCollection<Employee>()

// add some employees

list = list.OrderBy(emp => emp.Salary).ToObservableCollection();
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×