2009-10-19 78 views
2

我在ListView上使用ItemsSource。我现在想要执行搜索并用SQL查询过滤项目,所以我重新指定了ItemsSource;但随后会抛出异常: 在使用ItemsSource之前,项目集合必须为空。 好的,但我怎样才能清空项目集合?使用的ItemsSource时Items.Clear不允许..重新分配ItemsSource - 项目集合必须为空之前使用ItemsSource

好了,代码如下:

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: 
+0

你可以粘贴一个堆栈跟踪和一些你如何操作ItemsSource的示例代码吗? – 2009-10-19 18:16:23

+0

好的,我添加了它。 – eWolf 2009-10-19 19:22:01

回答

3

这通常是你设置ItemsSource属性的标志和你操纵的ItemsControl本身内的实际元素在XAML意味着要么你把东西或有手动将Items添加到ItemsControl中。

相关问题