【问题标题】:Reassign ItemsSource - Items collection must be empty before using ItemsSource重新分配 ItemsSource - 在使用 ItemsSource 之前,项目集合必须为空
【发布时间】:2010-12-08 02:09:48
【问题描述】:

我在 ListView 上使用 ItemsSource。我现在想使用 SQL 查询执行搜索和过滤项目,所以我重新分配 ItemsSource;但随后抛出异常: 在使用 ItemsSource 之前,项目集合必须为空。 好的,但是如何清空项目集合?使用 ItemsSource 时不允许使用 Items.Clear..

好的,代码如下:

this.Songs.ItemsSource = this.Library.ExecuteQuery<Songs>("SELECT * FROM songs WHERE title LIKE '%" + search + "%' OR artist LIKE '%" + search + "%' OR album LIKE '%" + search + "%'");

每次更改搜索框中的文本时都会执行这段代码。 以下是异常详情:

    System.InvalidOperationException was unhandled
  Message="Items collection must be empty before using ItemsSource."
  Source="PresentationFramework"
  StackTrace:
       bei System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value)
       bei System.Windows.Controls.ItemsControl.OnItemsSourceChanged(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, OperationType operationType)
       bei System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, OperationType operationType, Boolean isInternal)
       bei System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       bei System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
       bei Musiclibrary.Window1.Search_TextChanged(Object sender, TextChangedEventArgs e) in C:\Dokumente und Einstellungen\v. Wurstemberger\Eigene Dateien\Visual Studio 2008\Projects\Musiclibrary\Musiclibrary\Window1.xaml.cs:Zeile 132.
       bei System.Windows.Controls.TextChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       bei System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       bei System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       bei System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       bei System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       bei System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       bei System.Windows.Controls.Primitives.TextBoxBase.OnTextChanged(TextChangedEventArgs e)
       bei System.Windows.Controls.Primitives.TextBoxBase.OnTextContainerChanged(Object sender, TextContainerChangedEventArgs e)
       bei System.Windows.Controls.TextBox.OnTextContainerChanged(Object sender, TextContainerChangedEventArgs e)
       bei System.Windows.Documents.TextContainerChangedEventHandler.Invoke(Object sender, TextContainerChangedEventArgs e)
       bei System.Windows.Documents.TextContainer.EndChange(Boolean skipEvents)
       bei System.Windows.Documents.TextContainer.System.Windows.Documents.ITextContainer.EndChange(Boolean skipEvents)
       bei System.Windows.Documents.TextRangeBase.EndChange(ITextRange thisRange, Boolean disableScroll, Boolean skipEvents)
       bei System.Windows.Documents.TextRange.System.Windows.Documents.ITextRange.EndChange(Boolean disableScroll, Boolean skipEvents)
       bei System.Windows.Documents.TextRange.ChangeBlock.System.IDisposable.Dispose()
       bei System.Windows.Documents.TextEditorTyping.DoTextInput(TextEditor This, String textData, Boolean isInsertKeyToggled, Boolean acceptControlCharacters)
       bei System.Windows.Documents.TextEditorTyping.TextInputItem.Do()
       bei System.Windows.Documents.TextEditorTyping.ScheduleInput(TextEditor This, InputItem item)
       bei System.Windows.Documents.TextEditorTyping.OnTextInput(Object sender, TextCompositionEventArgs e)
       bei System.Windows.Controls.Primitives.TextBoxBase.OnTextInput(TextCompositionEventArgs e)
       bei System.Windows.UIElement.OnTextInputThunk(Object sender, TextCompositionEventArgs e)
       bei System.Windows.Input.TextCompositionEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       bei System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       bei System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       bei System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       bei System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       bei System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       bei System.Windows.Input.InputManager.ProcessStagingArea()
       bei System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       bei System.Windows.Input.TextCompositionManager.UnsafeCompleteComposition(TextComposition composition)
       bei System.Windows.Input.TextCompositionManager.PostProcessInput(Object sender, ProcessInputEventArgs e)
       bei System.Windows.Input.InputManager.ProcessStagingArea()
       bei System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       bei System.Windows.Input.TextCompositionManager.UnsafeStartComposition(TextComposition composition)
       bei System.Windows.Input.TextCompositionManager.PostProcessInput(Object sender, ProcessInputEventArgs e)
       bei System.Windows.Input.InputManager.ProcessStagingArea()
       bei System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       bei System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       bei System.Windows.Interop.HwndKeyboardInputProvider.ProcessTextInputAction(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       bei System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
       bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       bei System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       bei System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       bei System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       bei System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
       bei System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG& msg, Boolean& handled)
       bei System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
       bei System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       bei System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       bei System.Windows.Threading.Dispatcher.Run()
       bei System.Windows.Application.RunDispatcher(Object ignore)
       bei System.Windows.Application.RunInternal(Window window)
       bei System.Windows.Application.Run(Window window)
       bei System.Windows.Application.Run()
       bei Musiclibrary.App.Main() in C:\Dokumente und Einstellungen\v. Wurstemberger\Eigene Dateien\Visual Studio 2008\Projects\Musiclibrary\Musiclibrary\obj\Debug\App.g.cs:Zeile 0.
       bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

【问题讨论】:

  • 您能否粘贴堆栈跟踪和一些您如何操作 ItemsSource 的示例代码?

标签: sql wpf linq listview binding


【解决方案1】:

这通常表明您正在设置 ItemsSource 属性并且您已在 ItemsControl 本身中操作了实际元素,这意味着您已将内容放入 XAML 或已手动将子项添加到 ItemsControl。

【讨论】:

    猜你喜欢
    • 2012-07-24
    • 2010-10-15
    • 1970-01-01
    • 2012-03-04
    • 2013-08-08
    • 2012-08-02
    • 2014-01-07
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多