1

私のアプリケーションは WPF を使用していDataGridます。列の 1 つは、行をフィードするエンティティのへのComboBoxバインドを含むテンプレート列です。ObservableCollectionに値を追加するとObservableCollection、 aNullReferenceExceptionがスローされます。

なぜこれが起こるのか誰にも分かりますか?例外のスタック トレースは次のとおりです。

   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 args) で
   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 args) で
   System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex で (NotifyCollectionChangedEventArgs 引数、Int32 調整されたOldIndex、Int32 調整されたNewIndex)
   System.Windows.Data.ListCollectionView.ProcessCollectionChanged (NotifyCollectionChangedEventArgs args) で
   System.Windows.Data.CollectionView.OnCollectionChanged (オブジェクトの送信者、NotifyCollectionChangedEventArgs args) で
   System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged (NotifyCollectionChangedEventArgs e) で
   System.Collections.ObjectModel.ObservableCollection`1.InsertItem (Int32 インデックス、T アイテム) で
   System.Collections.ObjectModel.Collection`1.Add(T item) で
   C:\Project\Phoenix\Development\src\ORF.PersonBook.IdentityModule\Model\SubsidiaryModel.cs:line 127 の ORF.PersonBook.IdentityModule.Model.SubsidiaryModel.AddRoom(RoomModel room) で
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);
}

解決策は、実際にリストからアイテムを削除する前に、最初に選択したアイテムを null (または他のアイテム) に設定することです。

オリバー・ハナッピ

于 2010-06-06T21:45:59.453 に答える
0

Room クラス オブジェクトは実際にインスタンス化されていますか? 初期化されていない値を追加したように見えます。

于 2009-10-12T09:31:52.937 に答える