为什么ObservableCollection在修改时会引发异常? [英] Why does ObservableCollection throws an exception when being modified?

查看:177
本文介绍了为什么ObservableCollection在修改时会引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用WPF DataGrid 。其中一列是一个模板列,其中包含绑定到提供该行的实体的 ObservableCollection ComboBox 。当我向 ObservableCollection 添加一个值时,会抛出 NullReferenceException


$ b有没有人知道为什么会发生这种情况?以下是异常的堆栈跟踪:

 
在MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid()
在MS。 Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate()
在MS.Internal.Data.ClrBindingWorker.get_IsDBNullValidForUpdate()
在System.Windows.Data.BindingExpression.ConvertProposedValue(对象值)
在系统。 Windows.Data.BindingExpressionBase.UpdateValue()
在System.Windows.Data.BindingExpression.Update(布尔同步)
在System.Windows.Data.BindingExpressionBase.Dirty()
在系统。 System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp,Object value,PropertyMetadata metadata,Boolean coerceWithDeferredReference,OperationType operationType,Boolean isInternal)

$ b在System.Windows.DependencyObject.SetValue(DependencyProperty dp,Object value)
在System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties()
在System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
在System.Windows.Controls.Primitives.Selector.OnItemsChanged (NotifyCollectionChangedEventArgs e)
在System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)
在System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender,NotifyCollectionChangedEventArgs e)
在System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
在System.Windows.Controls.ItemCollection.System.Windows.IWeakEventListener.ReceiveWeakEvent(类型managerType,对象发件人,EventArgs e)
在系统.Windows.WeakEventManager.DeliverEventToList(Object sender,EventArgs args,ListenerList list)
在System.Windows.WeakEventManager.DeliverEvent(Object sender,EventArgs args)
在System.Colle ctions.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs args)
在System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
在System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustOldIndex,Int32 adjustNewIndex)
在System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
在System.Windows.Data.CollectionView.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs args)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
在System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32索引,T项)
在System.Collections.ObjectModel.Collection `1.Add(T item)
在ORF.PersonBook.IdentityModule.Model.SubsidiaryModel.AddRoom(Roo mModel房间)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 =removeDockPanel.Dock =RightCommand ={Binding Some Remove Command}/>
< TextBlock Text ={Binding Name}/>
< / DockPanel>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>

视图模型:

  //查看模型
public ObservableCollection< Room>客房{get;私人集}
public Room SelectedRoom {get;组; }

public RemoveRoom()
{
var room = SelectedRoom;
// SelectedRoom = null; //取消注释此行以避免异常

Rooms.Remove(room);
}

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



Oliver Hanappi


My application uses a WPF DataGrid. One of the columns is a template column that contains a ComboBox bound to an ObservableCollection of the entity which feeds the row. When I add a value to the ObservableCollection, a NullReferenceException is thrown.

Has anybody an idea why this happens? Here is the stack trace of the exception:

   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

解决方案

Finally I found the reason for the exception. The problem occurs when you are removing the selected item from the list. Below I've posted an incomplete piece of code.

<!-- 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>

And the view model:

// 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);
}

The solution is to first set the selected item to null (or any other item) before actually removing the item from the list.

Oliver Hanappi

这篇关于为什么ObservableCollection在修改时会引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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