我在使用WPFToolkit中的手风琴控件时遇到了麻烦。该控件绑定到一个ObservableCollection。当我尝试从集合中删除在初始化集合后添加的项目时,出现ArgumentOutOfRangeException。仅当未选择该项目时才会发生。
这是我的代码:
private void AddNewReservation()
{
var res = new Reservation();
var rvm = new ReservationViewModel(res);
Reservations.Add(rvm);
rvm.DeleteCommand = new RelayCommand(
param => Reservations.Remove(rvm));
rvm.PropertyChanged += (sender, args) => SaveReservation(res, rvm);
}
我的目标是在ObesrvableCollection中添加一个包含空预订模型的ReservationViewModel。只要Model / ViewModel没有有效的内容,就不应将其添加到存储库中。
那么,我在这里做错什么了吗?或者这是wpftoolkit中的已知错误?
这是我的StackTrace:
bei System.ThrowHelper.ThrowArgumentOutOfRangeException()
bei System.Collections.Generic.List`1.get_Item(Int32 index)
bei System.Collections.ObjectModel.Collection`1.System.Collections.IList.get_Item(Int32 index)
bei System.Windows.Data.ListCollectionView.InternalItemAt(Int32 index)
bei System.Windows.Data.ListCollectionView.GetItemAt(Int32 index)
bei System.Windows.Controls.ItemCollection.GetItemAt(Int32 index)
bei System.Windows.Controls.ItemCollection.get_Item(Int32 index)
bei System.Windows.Controls.Accordion.UnselectItem(Int32 index, Object item)
bei System.Windows.Controls.Accordion.ChangeSelectedIndex(Int32 oldIndex, Int32 newIndex)
bei System.Windows.Controls.Accordion.OnSelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
bei System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
bei System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
bei System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
bei System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
bei System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
bei System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
bei System.Windows.Controls.Accordion.set_SelectedIndex(Int32 value)
bei System.Windows.Controls.Accordion.OnItemsChanged(NotifyCollectionChangedEventArgs e)
bei System.Windows.Controls.ItemsControl.OnItemCollectionChanged2(Object sender, NotifyCollectionChangedEventArgs e)
bei System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
bei System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
bei System.Windows.Controls.ItemCollection.OnViewCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
bei System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType)
bei System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args)
bei System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
bei System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
bei System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex)
bei System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
bei System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
bei System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
bei System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
bei System.Collections.ObjectModel.Collection`1.Remove(T item)
bei NORDWIND_RS.ViewModel.BookingViewModel.<>c__DisplayClass17.<AddNewReservation>b__15(Object param)
感谢您的任何建议,
最佳答案
我发现了一个黑客,现在可以解决这个问题,而且收藏数量很少,但是这不应该这样。因此,如果某人发现更好的东西,请告诉我。
private void AddNewReservation()
{
var res = new Reservation();
var rvm = new ReservationViewModel(res);
Reservations.Add(rvm);
rvm.DeleteCommand = new RelayCommand(
param =>
{
Reservations = new ObservableCollection<ReservationViewModel>(Reservations.Where(r => r != rvm));
});
rvm.PropertyChanged += (sender, args) => SaveReservation(res, rvm);
}