How to sort an ObservableCollection<KeyValuePair<int,
本文关键字:lt KeyValuePair int to sort ObservableCollection How an | 更新日期: 2023-09-27 18:33:01
我有一个名为 TestList 的ObservableCollection<KeyValuePair<int, String>>()
绑定在文本框上,我想按int
对集合进行排序。我已经尝试了以下内容,但它没有对集合进行排序:
new ObservableCollection<KeyValuePair<int, string>>(
TestList .Where(p => p.Key > 0)
.GroupBy(p => p.Key)
.Select(grp => grp.First())
.OrderBy(p => p.Key)
);
如何对馆藏进行排序?绑定是否仍然有效?
编辑(也不起作用):
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));
}
和图形用户界面:
<TextBox Grid.Column="0" IsReadOnly="True"
Text="{Binding Path=Value , Mode=OneWay}" />
您不必按方式分组。您只需要一个简单的订单。
TestList.OrderBy(p => p.Key)
由于源包含KeyValuePair
对象,因此您可能会假定键已删除重复数据。因此,分组没有用。只要保持OrderBy
,可能还有你的Where
,你应该没事。
new ObservableCollection<KeyValuePair<int, string>>(
TestList.Where(p => p.Key > 0)
.OrderBy(p => p.Key)
);