Click here to Skip to main content
13,898,048 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to use the filter property of ICollectionView object in WPF databinding. Code below -

ICollectionView view = CollectionViewSource.GetDefaultView(GridProductInformation.DataContext);
int supplierID = GetComboBoxSelectedItem();
//Predicate<object> pred = new Predicate<object>(MatchSupplier);
Predicate<object> pred = delegate(object obj)
    NorthwindProduct _product = new NorthwindProduct();
    _product = obj as NorthwindProduct;
    int _supplierID = Convert.ToInt16(_product.SupplierID);
    if (_supplierID == supplierID)
        return true;
        return false;

The code throwsa run time exception in the last line - Null reference exception. Object reference not set to instance of an object.

Any idea what is going wrong?
Updated 1-Oct-10 11:11am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

@anshudutta their is a casting problem itseems. Try setting a breakpoint at
_product = obj as NorthwindProduct;
I think this is always null.

Evaluate, what
which is coming inside your predicate.
anshudutta 1-Oct-10 0:06am
I thought that initially, then I commented the code in my predicate and just wrote return True . It still throws the error.
PumbaPumba 1-Oct-10 0:09am
Do you got any samples for this to re-product. May be I'll give it a try.
anshudutta 1-Oct-10 0:21am
Well I am binding to a northwind database.. product table and trying to filter products by supplier ids. The combobox is bound to collection of supplier ids. When a particular supplier id is selected, the binding should filter products based on that supplier id. Cant understand why is it throwing Null exception
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Apologies if you've found the answer already, but this if probably what you need ...
view.Filter = pred;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190306.1 | Last Updated 25 Apr 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100