1

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

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

   在 MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid()
   在 MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate()
   在 MS.Internal.Data.ClrBindingWorker.get_IsDBNullValidForUpdate()
   在 System.Windows.Data.BindingExpression.ConvertProposedValue(对象值)
   在 System.Windows.Data.BindingExpressionBase.UpdateValue()
   在 System.Windows.Data.BindingExpression.Update(布尔同步)
   在 System.Windows.Data.BindingExpressionBase.Dirty()
   在 System.Windows.Data.BindingExpression.SetValue(DependencyObject d,DependencyProperty dp,对象值)
   在 System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp、对象值、PropertyMetadata 元数据、布尔 coerceWithDeferredReference、OperationType operationType、布尔 isInternal)
   在 System.Windows.DependencyObject.SetValue(DependencyProperty dp,对象值)
   在 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(对象发送者,NotifyCollectionChangedEventArgs e)
   在 System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(对象发送者,NotifyCollectionChangedEventArgs e)
   在 System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs 参数)
   在 System.Windows.Controls.ItemCollection.System.Windows.IWeakEventListener.ReceiveWeakEvent(类型 managerType,对象发送者,EventArgs e)
   在 System.Windows.WeakEventManager.DeliverEventToList(对象发送者,EventArgs 参数,ListenerList 列表)
   在 System.Windows.WeakEventManager.DeliverEvent(对象发送者,EventArgs 参数)
   在 System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(对象发送者,NotifyCollectionChangedEventArgs 参数)
   在 System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs 参数)
   在 System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs 参数,Int32 调整的OldIndex,Int32 调整的NewIndex)
   在 System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs 参数)
   在 System.Windows.Data.CollectionView.OnCollectionChanged(对象发送者,NotifyCollectionChangedEventArgs 参数)
   在 System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   在 System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 索引,T 项)
   在 System.Collections.ObjectModel.Collection`1.Add(T 项)
   在 ORF.PersonBook.IdentityModule.Model.SubsidiaryModel.AddRoom(RoomModel room) 在 C:\Project\Phoenix\Development\src\ORF.PersonBook.IdentityModule\Model\SubsidiaryModel.cs:line 127
4

2 回答 2

10

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

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

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

奥利弗·哈纳皮

于 2010-06-06T21:45:59.453 回答
0

您的 Room 类对象是否实际实例化?看起来好像您添加了一个未初始化的值。

于 2009-10-12T09:31:52.937 回答