2009-10-19 26 views
2

ListView에서 ItemsSource를 사용하고 있습니다. 이제는 검색을 수행하고 SQL 쿼리로 항목을 필터링하여 ItemsSource를 다시 할당하려고합니다. 그런 다음 예외가 발생합니다. ItemsSource를 사용하려면 항목 컬렉션이 비어 있어야합니다. 좋아요,하지만 아이템 컬렉션을 비우려면 어떻게해야합니까? ItemsSource를 사용하는 경우 Items.Clear는ItemsSource 재 할당 - ItemsSource를 사용하기 전에 Items 컬렉션이 비어 있어야합니다.

좋아, 코드는 다음과 같습니다 .. 사용할 수 없습니다 : 코드의

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를 조작하는 방법에 대한 샘플 코드를 붙여 넣기 바랍니다. –

+0

좋아, 나는 그것을 추가했다. – eWolf

답변

3

이 보통이 ItemsSource 속성을 설정하는 표시이다 그리고 당신은 XAML에 하나 당신이 넣어 한 것을 의미 ItemsControl에 자체 내에서 실제 요소를 조작하거나이있다 ItemsControl에 Children을 수동으로 추가했습니다.

관련 문제