我得到这样一个例外:“指定参数超出有效values.Parameter名的范围:指数”增加新的项目时的ObservableCollection <T>
“FooStorageStorage.Add(新树型视图(){头= i.ToString()})” 投掷类型的异常 '' System.ArgumentOutOfRangeException: “指定参数超出有效的值的范围的\ r \ n参数 名称:索引”
我在viewModel中有一个属性:
private ObservableCollection<TreeViewItem> fooStorage=new ObservableCollection<TreeViewItem>();
public ObservableCollection<TreeViewItem> FooStorage
{
get { return facetStorage; }
set { facetStorage = value; }
}
然而,当我清理FooStorage
,并试图增加新的项目:
private void LoadData()
{
if (FooStorage.Count > 0)
{
FooStorage.Clear();
}
for (int k = 0; k < lengthOfColl; k++)
{
FooStorage.Add(new TreeViewItem() { Header=k.ToString()});//here is exception
}
}
我有一个以上的异常。
当我第一次调用方法LoadData()时,它一切正常。然后,如果我第二次调用方法LoadData(),那么我会遇到这样的异常。
有没有人遇到这样的例外?最有趣的是,我无法在测试项目中重现此异常。
堆栈跟踪:
System.ArgumentOutOfRangeException了未处理的HResult = -2146233086 消息=指定参数超出有效的值的范围的。 参数名称:索引= PARAMNAME索引源= PresentationCore
堆栈跟踪: 在System.Windows.Media.VisualCollection.Insert(的Int32指数,可视视觉) 在System.Windows.Controls.UIElementCollection.InsertInternal(的Int32 索引,的UIElement (System.Windows.Controls.Panel.AddNildren(GeneratorPosition pos,Int32 itemCount) )在System.Windows.Controls.Panel.OnItemsChangedInternal(Object sender,ItemsChangedEventArgs args) 在System.Windows.Controls.Panel.OnItemsChanged(对象发件人,ItemsChangedEventArgs参数) at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item,Int32 index) 在System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(对象 发件人,NotifyCollectionChangedEventArgs参数) 在System.Windows.WeakEventManager.ListenerList1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Controls.ItemCollection.OnViewCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.WeakEventManager.ListenerList
1.DeliverEvent(对象 发件人,EventArgs的,类型managerType) 在System.Windows.WeakEventManager。使用deliverEvent(对象发件人,EventArgs参数) 在System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(对象 发件人,NotifyCollectionChangedEventArgs参数) 在System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs 参数) 在System.Windows.Data .ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs 指定参数时,的Int32 adjustedOldIndex,的Int32 adjustedNewIndex) 在System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs 参数) 在System.Windows.Data.CollectionView.OnCollectionChanged(对象发件人, NotifyCollectionChangedEventArgs参数) System.Collections中在。 ObjectModel。ObservableCollection1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection
1.InsertItem(Int32 index,T item) at System.Collections.ObjectModel.Collection1.Add(T item) at ModuleA.ViewModel.PersonControlViewModel.LoadData(IPerson person) in D:\WPF\...\ViewModel\PersonControlViewModel.cs:line 110 at Prism.Commands.DelegateCommand
1. <> c__DisplayClass1_0。 <。> b__0(对象 o) 在Prism.Commands.DelegateCommandBase。 <> c__DisplayClass5_0。 < .ctor> b__0在Prism.Commands.DelegateCommandBase.d__14.MoveNext() ---从先前位置堆栈跟踪结束(对象 ARG) 其中抛出异常--- 在System.Runtime.CompilerServices.TaskAwaiter .ThrowForNonSuccess(任务 任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) 在Prism.Commands.DelegateCommandBase.d__12.MoveNext() ---从以前的位置,其中的例外是堆栈跟踪的结尾抛出--- 在System.Runtime.CompilerServices.AsyncMethodBuilderCore。 <> c.b__6_0在System.Windows.Threading.ExceptionWrapper.InternalRealCall(对象 状态) (代表 回调,对象指定参数时,的Int32 numArgs) 在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源, 代表回调,对象指定参数时,的Int32 numArgs,代表catchHandler) 在System.Windows.Threading.DispatcherOperation.InvokeImpl() 在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象 状态) 在System.Threading.ExecutionContext.RunInternal(的ExecutionContext executionContext,ContextCallback回调,对象状态,布尔 preserveSyncCtx) at Sys在MS.Internal.CulturePreservingExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)上执行tem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) CulturePreservingExecutionContext 的ExecutionContext,ContextCallback回调,对象状态) 在System.Windows.Threading.DispatcherOperation.Invoke() 在System.Windows.Threading.Dispatcher.ProcessQueue() 在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr的HWND ,Int32 msg,IntPtr wParam,IntPtr lParam,布尔&处理) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam, IntPtr的lParam的,布尔&处理) 在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象O) 在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表 回调,对象指定参数时,的Int32 numArgs) 在System.Windows.Threading程序。 ExceptionWrapper.TryCatchWhen(对象源, 代表回调,对象指定参数时,的Int32 numArgs,代表catchHandler) 在System.Windows.Threading.Dispatcher.LegacyInvokeImpl(的DispatcherPriority 优先权,时间跨度超时,委托方法,对象指定参数时,的Int32 numArgs) 在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMess年龄(MSG & MSG) 在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 帧) 在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame帧) 在System.Windows.Application.RunDispatcher(对象忽略) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application。运行() at PrototypeBootstrapper.App.Main()in D:\ WPF ... \ Src \ PrototypeBootstrapper \ obj \ Debug \ App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [ ]参数) 在System.AppDomain.ExecuteAssembly(字符串assemblyFile,证据assemblySecurity,字串[] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart()的InnerException:
更新:
我我将从FooStorage
添加到放置在DataGridTextBoxColumn
的HeaderTemplate中的不同TreeView
。因此,当用户点击Header1
的DataGridTextBoxColumn2
时,Header1
的TreeView
由来自viewModel的FooStorage
填充。好。该当用户点击在DataGridTextBoxColumn2
Header2
,那么Header2
TreeView
应由FooStorage
从视图模型填充,但是当我Clear()
和Add()
新项目FooStorage
,然后我有一个以上的异常。
这里也发布调用堆栈。 – MoonKnight
添加时触发的任何事件? –
请提及ObservableCollection任何事件的所有订阅者。很显然,其他代码在Clear()或Add()方法上做错了。阅读[问],提供[mcve]。 – CodeCaster