简体   繁体   English

如何对ObservableCollection进行排序<KeyValuePair<int, string>

[英]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.

相关问题 如何重新编号ObservableCollection <KeyValuePair<int, string> - How to renumber an ObservableCollection<KeyValuePair<int, string> 如何将ListView绑定到ObservableCollection <KeyValuePair<int,object> &gt;在WPF中的ViewModel中? - How to bind a ListView to an ObservableCollection<KeyValuePair<int,object>> inside a ViewModel in WPF? 如何将值设置为:KeyValuePair <KeyValuePair <String,String>,int> - How to set the values to: KeyValuePair<KeyValuePair<String,String>,int> 复制ObservableCollection <myclass> 到ObservableCollection <KeyValuePair<string,object> &gt; - Copy ObservableCollection<myclass> to ObservableCollection<KeyValuePair<string,object>> 排序列表 <KeyValuePair<int,string> &gt;按C键 - To Sort a List <KeyValuePair<int,string>> by key in c sharp 如何正确使用列表上的LINQ查询 <KeyValuePair<string, string> 创建字典 <KeyValuePair<string, string> ,int&gt;? - How to properly use LINQ Query on List<KeyValuePair<string, string> to create Dictionary<KeyValuePair<string, string>, int>? 如何将int数组转换为List <KeyValuePair<int, string> &gt;? - How to convert int array to List<KeyValuePair<int, string>>? 绑定到ObservableCollection的“值” <KeyValuePair<object, string> &gt; - Bind to the “value” of ObservableCollection<KeyValuePair<object, string>> 排序列表 <keyValuePair<string,string> &gt; - Sort list<keyValuePair<string,string>> 如何对KeyValuePair的ComboBox.Items集合进行排序 <string,string> 按价值? - How to sort a ComboBox.Items collection of KeyValuePair<string,string> by Value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM