繁体   English   中英

指定的参数超出有效值范围。 参数名称索引。 由ObservableCollection.Add()方法抛出

[英]Specified argument was out of the range of valid values. parameter name index. thrown by ObservableCollection.Add() method

所以在我的应用程序中,我需要显示一些图像,为此,我使用了像这样绑定到ObservableCollection<Image> ItemsControlUniformGrid数据

<ItemsControl Name="ImageItemsControl" ItemsSource="{Binding Path=ObservableCollectionSource}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <UniformGrid Rows="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                        </UniformGrid>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>   

当我刚启动程序时,它工作得很好,但是在某个时候,我需要从网格中卸载所有图像。 因为如果我在集合上调用.Clear()会破坏我的绑定,请像这样一个接一个地删除所有元素:

int limit = ObservableCollectionSource.Count -1;
                for (int i = limit; i >= 0; i--)
               {
                    ObservableCollectionSource.RemoveAt(i);
               }

乍看之下它似乎可以正常工作,但是当我需要将图像添加回网格中时(通过拖放操作,或者从打开文件对话框中-结果相同)。 它抛出此异常

指定的参数超出有效值范围。 参数名称索引

在我调用.Add()方法的那一行。

Image img = CreateImage(cImg);
                    try
                    {
                       ObservableCollectionSource.Add(img);
                    }
                    catch (Exception ex )
                    {
                        MessageBox.Show("Exception has occured: " + ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }    

在这里,我从文件加载的位图中创建带有必要“设置”的wpf图像,并尝试将其添加到我的ObservableCollection中。

有什么想法为什么不起作用,或者我需要进行研究的方向?

谷歌搜索没有产生任何结果(也许我只是对搜索查询的表述不佳),而且我全都没主意。

我绑定错误吗? 我是否对任务使用错误的控件?

我不知道。

任何帮助是极大的赞赏

编辑:这是我得到的异常的堆栈跟踪

at System.Windows.Media.VisualCollection.Insert(Int32 index, Visual visual)
   at System.Windows.Controls.Panel.AddChildren(GeneratorPosition pos, Int32 itemCount)
   at System.Windows.Controls.Panel.OnItemsChangedInternal(Object sender, ItemsChangedEventArgs args)
   at System.Windows.Controls.Panel.OnItemsChanged(Object sender, ItemsChangedEventArgs args)
   at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index)
   at System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
   at System.Windows.WeakEventManager.ListenerList`1.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(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.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 MainDesk.MainWindow.AddImagesToGrid() in D:\MainDesk\MainDesk\UI\MainWindow.xaml.cs:line 427
   at MainDesk.MainWindow.AddCarrierImageBtn_Click(Object sender, RoutedEventArgs e) in D:\MainDesk\MainDesk\UI\MainWindow.xaml.cs:line 167
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at 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)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at MainDesk.App.Main() in D:\MainDesk\MainDesk\obj\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at 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)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

所以看来我弄清楚了我的错误在哪里。

首先。 关于.Clear()打破了我的束缚。 -我完全忘记了NotifyPropertyChanged 但是,当我弄清楚这部分时,异常仍然存在。

现在到主要部分-我高估了我的应用程序的简单性,结果没有发布足够的相关信息。 之后我无法重现在一个较小的应用程序,这是一无所有,但一个问题ObservableCollection和一个ItemsPanelUniformGrid它。 我知道问题出在别的地方。

事实证明,由于某种原因,我同时将Image添加到有问题的ObservableCollection中,同时还将此图像添加到另一个ObservableCollection ,该ObservableCollection是另一个ItemsPanel ItemsSource 两个面板是相同的,但是彼此不了解,并且没有以任何方式连接。

事情是,其中之一就是将Visibility作为Visibility.Collapsed当我向ObservableCollection添加项目时,此方法ObservableCollection

现在,当我一次只使用一个ObservalbeCollection时,问题就消失了。

尽管我仍然不知道是什么原因导致了这种行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM