[英]How to sort an ObservableCollection<KeyValuePair<int, string>
I've an ObservableCollection<KeyValuePair<int, String>>()
named TestList binded on a Textbox and I want to sort the collection by int
. 我有一个名为TestList的
ObservableCollection<KeyValuePair<int, String>>()
绑定在文本框上,我想按int
排序集合。 I've tried the following but it doesn't sorted the collection: 我尝试了以下操作,但未对集合进行排序:
new ObservableCollection<KeyValuePair<int, string>>(
TestList .Where(p => p.Key > 0)
.GroupBy(p => p.Key)
.Select(grp => grp.First())
.OrderBy(p => p.Key)
);
How can I sort the collection? 如何分类收藏? And will the Binding still works?
绑定仍然有效吗?
EDIT (doesn't work too): 编辑(也不起作用):
public ObservableCollection<KeyValuePair<int, String>> TestList
{
get { return testList; }
set {
testList = value;
NotifyPropertyChanged("TestList");
}
}
public void Test(int index)
{
TestList.RemoveAt(index);
TestList = new ObservableCollection<KeyValuePair<int, string>>(TestList.OrderBy(p => p.Key));
}
and GUI: 和GUI:
<TextBox Grid.Column="0" IsReadOnly="True"
Text="{Binding Path=Value , Mode=OneWay}" />
You don't have to make a group by. 您不必分组。 You just need a simple order by.
您只需要简单的订购即可。
TestList.OrderBy(p => p.Key)
Since your source contains KeyValuePair
objects, you might assume that the keys are already deduplicated. 由于您的源包含
KeyValuePair
对象,因此您可以假定这些密钥已被重复数据删除。 Hence, grouping has no use. 因此,分组没有用。 Just keep the
OrderBy
and possibly your Where
and you should be fine. 只要保持
OrderBy
,可能你Where
,你应该罚款。
new ObservableCollection<KeyValuePair<int, string>>(
TestList.Where(p => p.Key > 0)
.OrderBy(p => p.Key)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.