Filtering a GridView in Windows 8

孤街醉人 提交于 2020-01-02 08:11:52

问题


I currently have a GridView where the ItemsSource is set to an ObservableCollection. Updates to the underlying data are reflected in the UI without any effort on my part. Things are working well.

What is the cleanest way to apply a filter to the ObservableCollection so that only certain items are displayed? Ideally I don't want to actually remove items from the ObservableCollection, nor do I want to maintain two distinct collections because it will make keeping things in sync more challenging. CollectionViewSource looked promising but the Windows Store App implementation seems to lack the filtering capability (why MS????).


回答1:


I just got done dealing with this same problem in my own C# + XAML Windows 8 app. Any of these three open-source projects will give you the functionality you're after: Bindable LINQ, Obtics, and Continuous LINQ.

It's just as well that CollectionViewSource doesn't include the filtering functionality in Windows 8, since it's preferable to put such functionality into the View Model anyway. The main advantage of doing so is that, packaged as a Portable Class Library (PCL), your View Model (including filtering) will then be portable across your WPF, Silverlight, and Windows 8 projects.

Although the above three open source projects looked interesting to me, I accomplished this task by building on top of my own MVVM framework. My framework already supports sorting, so filtering was a natural addition. Adding it in was easy while using the Reactive API that I implemented last month. I haven't yet uploaded my MVVM framework's latest version that includes the filtering. Let me know if none of the above three open source projects suit you, so I can get around to uploading a bit sooner.




回答2:


Asuming you're using some kind of MVVM of the templates,using LINQ:

this.DefaultViewModel["GreaterThan10Items"] = originalList.Where(c => c.SomeProperty > 10)
    .Select(c => new {c.SomeProperty, c.OtherProperty});

Or maybe:

this.DefaultViewModel["Children"] = from query in originalList
                                     where query.age <10
                                     select new Person
                                     {
                                      age= query.age,
                                      name = query.Name
                                     };

Of course, the ItemViewSource must be linked to GreaterThan10Items and your GridView binded to that ItemViewSource




回答3:


You can use my ObservableComputations library. For example if you want bing filtered by specific type orders to GridView:

GridView.ItemsSource = OrdersObservableCollection
    .Filtering(o => o.Type == OrdersFilter.SelectedType)

Now GridView is automatically refreshed when OrdersObservableCollection changes or Order.Type property changes. Ensure Oreder class implements INotifyPropertyChanged.



来源:https://stackoverflow.com/questions/13780608/filtering-a-gridview-in-windows-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!