Wpf 为什么ObservableCollection在被修改时抛出异常?

Wpf 为什么ObservableCollection在被修改时抛出异常?,wpf,exception,observablecollection,Wpf,Exception,Observablecollection,我的应用程序使用WPF数据网格。其中一列是一个模板列,其中包含一个绑定到提供行的实体的ObservableCollection的组合框。当我向ObservaleCollection添加一个值时,会抛出一个NullReferenceException 有人知道为什么会这样吗?以下是异常的堆栈跟踪: at MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid() at MS.Internal.Data.Propert

我的应用程序使用WPF数据网格。其中一列是一个模板列,其中包含一个绑定到提供行的实体的ObservableCollection的组合框。当我向ObservaleCollection添加一个值时,会抛出一个NullReferenceException

有人知道为什么会这样吗?以下是异常的堆栈跟踪:

at MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid() at MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate() at MS.Internal.Data.ClrBindingWorker.get_IsDBNullValidForUpdate() at System.Windows.Data.BindingExpression.ConvertProposedValue(Object value) at System.Windows.Data.BindingExpressionBase.UpdateValue() at System.Windows.Data.BindingExpression.Update(Boolean synchronous) at System.Windows.Data.BindingExpressionBase.Dirty() at System.Windows.Data.BindingExpression.SetValue(DependencyObject d, DependencyProperty dp, Object value) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, OperationType operationType, Boolean isInternal) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties() at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e) at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Controls.ItemCollection.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e) at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex) at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at ORF.PersonBook.IdentityModule.Model.SubsidiaryModel.AddRoom(RoomModel room) in C:\Project\Phoenix\Development\src\ORF.PersonBook.IdentityModule\Model\SubsidiaryModel.cs:line 127
您的房间类对象是否实际实例化?
看起来好像您添加了一个未初始化的值。

您的房间类对象实际实例化了吗?
看起来您添加了一个未初始化的值。

最后我找到了异常的原因。从列表中删除所选项目时会出现问题。下面我发布了一段不完整的代码

<!-- XAML -->
<ListBox ItemsSource="{Binding Rooms}" SelectedItem="{Binding SelectedRoom}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Button Content="remove" DockPanel.Dock="Right" Command="{Binding Some Remove Command}" />
                <TextBlock Text="{Binding Name}" />
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
以及视图模型:

// View Model
public ObservableCollection<Room> Rooms { get; private set; }
public Room SelectedRoom { get; set; }

public RemoveRoom()
{
    var room = SelectedRoom;
    //SelectedRoom = null; // Uncomment this line to avoid the exception

    Rooms.Remove(room);
}
解决方案是首先将所选项目设置为null或任何其他项目,然后再从列表中实际删除该项目


奥利弗·哈纳皮(Oliver Hanappi)

我终于找到了异常的原因。从列表中删除所选项目时会出现问题。下面我发布了一段不完整的代码

<!-- XAML -->
<ListBox ItemsSource="{Binding Rooms}" SelectedItem="{Binding SelectedRoom}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Button Content="remove" DockPanel.Dock="Right" Command="{Binding Some Remove Command}" />
                <TextBlock Text="{Binding Name}" />
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
以及视图模型:

// View Model
public ObservableCollection<Room> Rooms { get; private set; }
public Room SelectedRoom { get; set; }

public RemoveRoom()
{
    var room = SelectedRoom;
    //SelectedRoom = null; // Uncomment this line to avoid the exception

    Rooms.Remove(room);
}
解决方案是首先将所选项目设置为null或任何其他项目,然后再从列表中实际删除该项目


Oliver Hanappi

你能举个例子重现问题吗?我希望我能,但我在一个小测试应用程序中尝试过,但无法重现问题:扫描你举个例子重现问题吗?我希望我能,但我在一个小测试应用程序中尝试过,但无法重现问题:SRoom不是null,尽管引发了异常,集合包含房间,因此,侦听ObservableCollection事件的数据绑定似乎会导致异常。不幸的是,我不知道在哪里,但我将尝试调试数据绑定,将其跟踪级别设置为高。@Oliverhanapi,噢,我爱你Oliver,你救了我的命!导致此异常的是数据绑定!Room不为null,尽管引发了异常,但集合包含该Room,因此侦听ObservableCollection事件的数据绑定似乎导致了异常。不幸的是,我不知道在哪里,但我将尝试调试数据绑定,将其跟踪级别设置为高。@Oliverhanapi,噢,我爱你Oliver,你救了我的命!导致此异常的是数据绑定!