开发者

How to filter Observable Collection Class Collection

I have implemented Linq-To-Sql.. A开发者_StackOverflowdd necessary table in it... after that linq class will automatically set property for field.. I implemented one class using ObservableCollection class.. and pass datacontextclass object in its constructor...

so after getting all data how to filter it?

  public class BindBookIssueDetails : ObservableCollection
        {
            public BindBookIssueDetails(DataClasses1DataContext dataDC)
            {
                foreach (Resource_Allocation_View res in dataDC.Resource_Allocation_Views)
                {
                    this.Add(res);
                }
            }
        }

private BindBookIssueDetails bResource;
bResource = new BindBookIssueDetails(db);
_cmbResource.ItemSource=bResource;

Please Help me.


You can use CollectionViewSource and filter it. So that it affect only at the View(.XAML) side

    ICollectionView collectionView = CollectionViewSource.GetDefaultView(bResource);
    collectionView.Filter = new Predicate<object>(YourFilterFunction);

Check out this blog for more details. http://bea.stollnitz.com/blog/?p=31


I tried to use @Jobi's solution but for some reason I got an exception trying to fire FilterFunction.

So I used a slightly different approach. I cast CollectionViewSource's DefaultView to a BindingListCollectionView

myVS=(BindingListCollectionView)CollectionViewSource.GetDefaultView(sourceofdata);

and now I can construct an SQL-like filter string and apply it like that:

myVS.CustomFilter=myfilterstring;

I will still try to resolve my problem (I presume @Jobi's solution is more flexible).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜