由字符串排序的ObservableCollection [英] Sorting ObservableCollection by String

查看:118
本文介绍了由字符串排序的ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观察的集合和集合的项目有一个标签(字符串)和器isChecked(布尔)。



用户可以选择一个标签排序列表如事件和/或设置器isChecked



的排序优先级应该像这样:
1)==器isChecked真
2)标签等于按标签的字母顺序排序的项目的项目标签
3)休息



我在展示通过XAML一个ListBox中的ObservableCollection。
不知道如何可以做到这一点?

 编辑:提供例如澄清:
item0:标签=苹果,=器isChecked虚假的;
ITEM1:标签=苹果,=器isChecked真实的;
ITEM2:标签=菠萝,=器isChecked虚假的;
项目3:标签=椰子,=器isChecked真实的;
ITEM4:标签=樱桃,=器isChecked虚假的;

字符串是:樱桃
成果:项目1,项目3,ITEM4,item0,ITEM2


解决方案

要能够检索你的情况,我没了下文。

  //检索的colleciton一个IQueryable与指定的条件
VAR的查询=从C中收集
排序依据c.IsChecked下降,c.Tag.Equals(樱桃)下降, c.obsTag
选择C;

//清除集合
收集=新的ObservableCollection< myCollectionObject>();
//你的IQueryable的结果
的foreach(myCollectionObject查询OBJ){
collection.Add(OBJ)更换收集;
}

如果你想这一切在同一行:

 收集=新的ObservableCollection< obsCol>(从C中收集
排序依据c.obsCheck下降,c.obsTag.Equals(樱桃)降,c.obsTag
选择C);


I have a Observable Collection and an item of the collection has a tag (string) and a isChecked (boolean).

The User can select a tag to sort the list e.g. "event" and/or set the IsChecked.

The Ordering priority should work like this: 1) isChecked == true 2) tag equals the item tag 3) rest of the items sorted alphabetically by tag

I show the ObservableCollection in a ListBox through xaml. Any idea how this can be achieved?

EDIT: providing example for clarification:
item0: tag = "apple", isChecked = false;
item1: tag = "apple", isChecked = true;
item2: tag = "pineapple", isChecked = false;
item3: tag = "coconut", isChecked = true;
item4: tag = "cherry", isChecked = false;

string is: "cherry"
outcome: item1, item3, item4, item0, item2

解决方案

To be able to retrieve your conditions, I did the below.

 // Retrieve an IQueryable for the colleciton with your specified conditions
 var query = from c in collection
             orderby c.IsChecked descending, c.Tag.Equals("cherry") descending, c.obsTag
             select c;

 // Clear the collection
 collection = new ObservableCollection<myCollectionObject>();
 // Replace the collection with your IQueryable results
 foreach(myCollectionObject obj in query) {
      collection.Add(obj);
 }

If you want it all in one line:

 collection = new ObservableCollection<obsCol>(from c in collection 
              orderby c.obsCheck descending, c.obsTag.Equals("cherry") descending, c.obsTag 
 select c);

这篇关于由字符串排序的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆