我的应用程序使用WPF DataGrid
。其中一列是一个模板列,其中包含ComboBox
绑定到为该行提供的实体的ObservableCollection
。当我向ObservableCollection
添加值时,会抛出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
答案 0 :(得分: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);
}
解决方法是在从列表中实际删除项目之前,首先将所选项目设置为null(或任何其他项目)。
Oliver Hanappi
答案 1 :(得分:0)
您的Room类对象是否实际实例化了? 看起来好像你添加了一个未初始化的值。