我在使用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);
}
我的目标是将包含空预留模型的ReservationViewModel添加到ObesrvableCollection。只要Model / ViewModel没有有效内容,就不应将其添加到Repositrory。 那么我在这里做错了什么,或者这是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)
我会感谢任何建议,
答案 0 :(得分:0)
我找到了一个解决现在和小集合问题的黑客,但这不应该是这样的。所以如果somone发现更好的东西,请告诉我。
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);
}