ICollectionView筛选器属性-空引用异常 [英] ICollectionView Filter property - Null reference exception

查看:97
本文介绍了ICollectionView筛选器属性-空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在WPF数据绑定中使用ICollectionView对象的filter属性.下面的代码-

 ICollectionView视图= CollectionViewSource.GetDefaultView(GridProductInformation.DataContext);
 int  SupplierID = GetComboBoxSelectedItem();
// 谓词< object> pred = new Predicate< object>(MatchSupplier); 
谓词< object> pred = 委托(对象 obj)
{
    NorthwindProduct _product =  NorthwindProduct();
    _product = obj  as  NorthwindProduct;
     int  _supplierID = Convert.ToInt16(_product.SupplierID);
    如果(_supplierID ==供应商ID)
        返回 其他
        返回 ;
};
view.Filter(pred);


代码在最后一行抛出运行时异常-空引用异常.对象引用未设置为对象的实例.

知道出什么问题了吗?

解决方案

@anshudutta看来,这是一个强制转换问题.尝试在

_product = obj as NorthwindProduct;

处设置断点,我认为这始终为空.

评估谓词中包含的

obj 

内容.

view.Filter = pred;


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;
    else
        return false;
};
view.Filter(pred);


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?

解决方案

@anshudutta their is a casting problem itseems. Try setting a breakpoint at

_product = obj as NorthwindProduct;

I think this is always null.

Evaluate, what

obj 

which is coming inside your predicate.


Apologies if you''ve found the answer already, but this if probably what you need ...

view.Filter = pred;


这篇关于ICollectionView筛选器属性-空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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