开发者

Filter ObservableCollection

开发者 https://www.devze.com 2023-04-03 08:06 出处:网络
I am very new to coding and am trying to learn to code for Windows phone. I am stuck at a problem since the last week and it is driving me crazy.

I am very new to coding and am trying to learn to code for Windows phone. I am stuck at a problem since the last week and it is driving me crazy.

I have an开发者_StackOverflow社区 ObservableCollection defined like this:

public ObservableCollection<Note> Items { get; private set; }

The Note class has two variables in it called Index and Category.

I want to filter the ObservableCollection so that I can choose a particular element from it.

Is there a simple way doing this? Any help would be highly appreciated!


you can do:

var myCollection = GetNoteCollection(...);

var result = myCollection.Where(w => w.Category.Equals("MyCategory"));


Update: These classes are no longer available, check out ObservableCollectionView

Original Answer
Try one of these classes: OrderedObservableCollection or FilteredObservableCollection

OriginalList = new ObservableCollection<Person>();
FilteredList = new OrderedObservableCollection<Person, int>(originalList, p => p.Age, a => a.Age >= 18);

The FilteredList contains only persons with an age >= 18 and all persons are sorted by age. The FilteredList will automatically be updated if something changes in OriginalList. If OriginalList is global and FilteredList is used on a page, you have to call FilteredList.Unload() to remove the event binding - this is needed that the garbage collector can free the FilteredList.

These classes are not fully tested, if you find a problem, please report it here.

BTW: I'm not happy with the Unload() method. Is it possible to add something like a weak event reference?


You're not saying what you want to filter it on. But regardless, look at LINQs Enumerable.Where and Enumerable.FirstOrDefault

Example:

var answerToLifeTheUniverseAndEverything = Items.FirstOrDefault(note => note.Index == 42)


I think what you are looking to use is a CollectViewSource. These are supported in Windows Phone 7.1. You can create it, wrap your ObservableCollection and set it as a bindable property in the ViewModel. I am about to try it myself. If you want to know how I get on let me know.


I think you should use ListCollectionView and List<> with your ObservableCollection property:

1-Binding ListCollectionView to the same data source as ObservableCollection as:

ListCollectionView lvs;
var note = from n in Note select n;
        lvs = new ListCollectionView(note.ToList <Note>());

2- create filter method:

public bool ItemIndexFilter(object obj)
    {
        Note note = obj as Note;
        return (note.Index>=10);
    }

3- Giving the Items property its filtered value:

lvs.Filter = new Predicate<object>(ItemIndexFilter);
List<Note> note_list = new List<Note>();
for (int i = 0; i < lvs.Count; i++)
     {                   
        note_list.Add((Note)lvs.GetItemAt(i));    
     }
var observe = new ObservableCollection<Note>(note_list);
items=observe;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号