【问题标题】:ListBox throwing SelectedItems error on right clickListBox 在右键单击时抛出 SelectedItems 错误
【发布时间】:2013-01-22 08:54:24
【问题描述】:

我正在使用 Lync SDK,并将 ContactSearchResultList (listBox) 与 SelectionMode="Single" 一起使用。

但是当我右键单击未选择的项目时,应用程序崩溃并显示以下堆栈跟踪:

System.InvalidOperationException wurde nicht behandelt.
  HResult=-2146233079
  Message=Can only change SelectedItems collection in multiple selection modes. Use SelectedItem in single select modes.
  Source=PresentationFramework
  StackTrace:
       bei System.Windows.Controls.Primitives.Selector.OnSelectedItemsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       bei System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
       bei System.Collections.ObjectModel.ObservableCollection`1.ClearItems()
       bei System.Windows.Controls.SelectedItemCollection.ClearItems()
       bei System.Collections.ObjectModel.Collection`1.Clear()
       bei Microsoft.Lync.Controls.UCListBoxItem.OnMouseRightButtonDown(MouseButtonEventArgs e)
       bei System.Windows.UIElement.OnMouseRightButtonDownThunk(Object sender, MouseButtonEventArgs e)
       bei System.Windows.Input.MouseButtonEventArgs.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.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       bei System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
       bei System.Windows.Input.MouseButtonEventArgs.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.RaiseTrustedEvent(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.InputProviderSite.ReportInput(InputReport inputReport)
       bei System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       bei System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       bei System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       bei MS.Win32.UnsafeNativeMethods.DispatchMessage(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 LyncPresenceBrowser.App.Main() in D:\_effexx\_Kunden\DFG\LyncPresenceBrowser\obj\x86\Debug\App.g.cs:Zeile 0.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

为什么会抛出这个错误?在我的代码中,我没有提到SelectedItems,我总是使用SelectedItem。我卡住了,请帮帮我:)

//这里是事件:

public void contactSearchResultList1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (contactSearchResultList1.SelectedItem != null && contactSearchResultList1.SelectedIndex != -1)
    {
        expander1.IsEnabled = true;
        expander2.IsEnabled = true;
        expander1.IsExpanded = true;
        showGroupMembersWindow();
     }
     else
     {
        expander1.IsEnabled = false;
        expander2.IsEnabled = false;
        //closeGroupMembersWindow();
        //closeContactCardWindow();
     }
}

【问题讨论】:

  • OnMouse 点击事件怎么样..?你能显示那个代码吗..?
  • 能否在条件检查前加个检查,看看contactSearchResultList1.SelectedItem.Count ..?
  • 试试这样的var count = (from item in contactSearchResultList1.Items where item.Selected select item).Count();
  • 您在代码顶部的 using 中是否使用了 System.Linq
  • 您应该能够获取或检查选定项目的数量并获得计数。该代码在我这边工作不知道为什么它在您这边不工作,除非您拥有的不是 ListBox项目

标签: c# listbox selecteditem lync lync-2010


【解决方案1】:

contactSearchResultList 上的SelectionMode 的默认值似乎是Extended。我改变了那个值,现在一切似乎都正常了...... 感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2011-11-21
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多