Wpf 在加载的事件中获取空引用异常,在此事件中访问子数据网格中SelectedItems的计数

Wpf 在加载的事件中获取空引用异常,在此事件中访问子数据网格中SelectedItems的计数,wpf,datagrid,nullreferenceexception,selecteditem,Wpf,Datagrid,Nullreferenceexception,Selecteditem,以下是异常堆栈的顶部: System.NullReferenceException: Object reference not set to an instance of an object. at xx.TblQcLotListSelectionChanged(Object sender, SelectionChangedEventArgs e) in ...\InternalEventHandlerQCPage.cs:line 441 at xx.QCLotLis

以下是异常堆栈的顶部:

    System.NullReferenceException: Object reference not set to an instance of an object.
     at xx.TblQcLotListSelectionChanged(Object sender, SelectionChangedEventArgs e) in ...\InternalEventHandlerQCPage.cs:line 441
     at xx.QCLotListPageLeftLoaded(Object sender, RoutedEventArgs e) in ...\InternalEventHandlerQCPage.cs:line 435
这是InternalEventHandlerQCPage.cs中提到的部分:

    private void QCLotListPageLeftLoaded(object sender, RoutedEventArgs e)
    {
        this.QCPage.QCShowRangesTblHitTest = true;
        this.QCPage.QCShowRangesTblEnabled = true;
        this.QCPage.LOTListRightTxtNameEnabled = false;
        this.QCPage.LOTListFieldEnabled = false;
        this.QCPage.LOTListNumberDateEnabled = false;

        TblQcLotListSelectionChanged(null, null); //line 435
    }

    private void TblQcLotListSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (this._QCLotListPageLeftXAML.tblLotList.SelectedItems.Count == 0) //line 441
        {
            this.QCPage.LOTListRightBtnUpEnabled = false;
            this.QCPage.LOTListRightBtnDownEnabled = false;

            this.QCPage.QCShowRangesBtnUpEnabled = false;
            this.QCPage.QCShowRangesBtnDownEnabled = false;

            ...
“this”是一个用户控件。 _QCLotListPageLeftXAML也是一个用户控件,用于TabControl内的页面 tblLotList是一个WPF数据网格 QCPage是一个依赖项对象,仅包含绑定的依赖项属性

当datagrid有300个项目时,我们得到了这个异常,这是我们设置的允许的最大值。datagrid有4列,所以没有那么重。。。此外,软件之前已经运行了一段时间,因此在软件启动时没有出现这种情况


你知道这里会出什么问题吗?对于DataGrid,SelectedItems可以为空?

如果错误仍然出现,请尝试执行以下操作:

if (this._QCLotListPageLeftXAML.tblLotList.SelectedItems != null &&
    this._QCLotListPageLeftXAML.tblLotList.SelectedItems.Count == 0)
{

}

问题解决了。另一种情况是,已发布、已编译的应用程序投诉堆栈中的另一行,而不是实际导致异常的那一行。

使用调试器并逐步检查未设置的对象!是的,不一定要选择某个项目,但该错误必须是“百万分之一”左右。我想复制时无法复制它:(当然,碰巧没有选择任何项目,但我们这里讨论的是SelectedItems,这是一个集合。我无法复制异常。几个月来,它一直很好,现在我们经历了这一次,但没有任何线索表明该行上什么可以为空。这个问题只能从理论上解决。我想不出任何可以为空的东西,但如果有人有任何想法,请分享!:)