0
我有一個問題,讓我的應用程序崩潰,我無法弄清楚爲什麼。當我將「SelectedIndex = 0」分配給DataGrid時,爲什麼會收到System.InvalidOperationException?
我的代碼是這樣的:
Private Sub SelectedFieldFK_TextChanged() Handles SelectedFieldFK.TextChanged
RefreshSelections()
End Sub
Private Sub RefreshSelections()
If Selections Is Nothing Then Exit Sub
Selections.SetBinding(DataGrid.ItemsSourceProperty, New Binding With {.XPath = "..."})
m_SelectionSorter.Refresh()
If Selections.HasItems Then Selections.SelectedIndex = 0 '*)
End Sub
我,蹦蹦跳跳地走使用箭頭鍵列表框。通過綁定「SelectedFieldFK」不斷更改其值,並且名爲「Selections」的DataGrid不斷更新。之後,選擇DataGrid的第一行。這適用於第一次出現時要顯示2個選擇。然後我繼續按下向下的箭頭並最終落在有一個選擇的項目上。一旦函數「RefreshSelctions()」結束,該應用程序崩潰:
System.InvalidOperationException:只能在多種選擇模式下更改SelectedItems集合。在單選模式下使用SelectedItem。
和堆棧跟蹤
at System.Windows.Controls.Primitives.Selector.OnSelectedItemsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
at System.Windows.Controls.SelectedItemCollection.InsertItem(Int32 index, Object item)
at System.Collections.ObjectModel.Collection`1.Add(T item)
at System.Collections.ObjectModel.Collection`1.System.Collections.IList.Add(Object value)
at System.Windows.Controls.Primitives.Selector.UpdateSelectedItems(InternalSelectedItemsStorage toAdd, InternalSelectedItemsStorage toRemove)
at System.Windows.Controls.Primitives.Selector.UpdateSelectedItems()
at System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties()
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(ItemInfo info, Boolean assumeInItemsCollection)
at System.Windows.Controls.ListBox.MakeSingleSelection(ListBoxItem listItem)
at System.Windows.Controls.ListBox.MakeKeyboardSelection(ListBoxItem item)
at System.Windows.Controls.ListBox.OnGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
at System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
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.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
at System.Windows.UIElement.Focus()
at System.Windows.Controls.ItemsControl.FocusItem(ItemInfo info, ItemNavigateArgs itemNavigateArgs)
at System.Windows.Controls.ListBox.FocusItem(ItemInfo info, ItemNavigateArgs itemNavigateArgs)
at System.Windows.Controls.ItemsControl.NavigateByLineInternal(Object startingItem, FocusNavigationDirection direction, FrameworkElement startingElement, ItemNavigateArgs itemNavigateArgs, Boolean shouldFocus, FrameworkElement& container)
at System.Windows.Controls.ItemsControl.NavigateByLine(ItemInfo startingInfo, FrameworkElement startingElement, FocusNavigationDirection direction, ItemNavigateArgs itemNavigateArgs)
at System.Windows.Controls.ItemsControl.NavigateByLine(FocusNavigationDirection direction, ItemNavigateArgs itemNavigateArgs)
at System.Windows.Controls.ListBox.OnKeyDown(KeyEventArgs e)
at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
at System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
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.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
at System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
at System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
at System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG& msg, Boolean& handled)
at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
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 DatenfKatCreator.Application.Main() in C:\Users\Christoph\Documents\Visual Studio 2010\Projects\DatenfKatCreator\DatenfKatCreator\obj\x86\Debug\Resource\XAML\Application.g.vb:Zeile 66.
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()
當我註釋行*)
出來,它的工作原理,但當然沒有選擇第一項。
我錯過了什麼嗎?
1),但我不想啓用多個選擇。爲什麼我應該將其設置爲我不想要的行爲? 2)我試過了,它不起作用。 3)從stacktrace中可以看到錯誤來自ListBox,而不是DataGrid。事實上,將列表框的選擇模式設置爲「擴展」或「多個」有助於 - 但我希望單獨選擇。現在怎麼辦? – LWChris
那麼,如果你不想MultipleSelections那麼錯誤消息建議使用不同的屬性'SelectedItem' – Steve
讓我清楚的步驟:1)我使用鍵盤來改變列表框的選擇。 2)代碼設置DataGrid的SelectedIndex屬性。 3)錯誤發生在ListBox的選擇被更新時(自動因爲我使用了箭頭鍵而不是我)。 但是,不要在完全無關的DataGrid上執行SelectedIndex-update,並且ListBox選擇更新執行得很好。換句話說:爲什麼更新DataGrid.SelectedIndex會導致ListBox的鍵盤引起的SelectedItem更新失敗? – LWChris