[英]sorting datagrid in silverlight using Observable Collection
I have an observable collection which is binded to datagrid... I want to sort the datagrid by clicking on header. 我有一个绑定到datagrid的可观察集合...我想通过单击标题对datagrid进行排序。 It's a dynamic data.
这是一个动态数据。 Here is my code
这是我的代码
namespace SLSortObservableCollection
{
public partial class MainPage : UserControl
{
//ObservableCollection<int> NumData = new ObservableCollection<int>();
// ObservableCollection<string> StrData = new ObservableCollection<string>();
public MainPage()
{
InitializeComponent();
ObservableCollection<int> NumData = new ObservableCollection<int>();
ObservableCollection<string> StrData = new ObservableCollection<string>();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Random ra = new Random();
for (int i = 0; i < 10; i++)
{
int num = ra.Next(1000);
NumData.Add(num);
}
try
{
dataGrid1.ItemsSource = null;
dataGrid1.ItemsSource = NumData;
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
private void button2_Click(object sender, RoutedEventArgs e)
{
try
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < 5; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
StrData.Add(builder.ToString());
}
dataGrid1.ItemsSource = StrData;
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
}
}
ObservableCollection does not support sorting at all. ObservableCollection根本不支持排序。 If this was not silverlight, you might be able to do something with CollectionView.
如果不是Silverlight,则可以使用CollectionView执行某些操作。
As it is, you will probably have to use a custom extension of SortableCollection. 照原样,您可能必须使用SortableCollection的自定义扩展。 There are several of these flying around, just search for "sortable observableCollection"
其中有几条飞来飞去,只需搜索“ sortable observableCollection”
Some implementations to get you started 一些使您入门的实现
http://kiwigis.blogspot.de/2010/03/how-to-sort-obversablecollection.html http://kiwigis.blogspot.de/2010/03/how-to-sort-obversablecollection.html
http://elegantcode.com/2009/05/14/write-a-sortable-observablecollection-for-wpf/ http://elegantcode.com/2009/05/14/write-a-sortable-observablecollection-for-wpf/
http://sortablecollection.codeplex.com/ http://sortablecollection.codeplex.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.