locked
System.ArgumentException selecting item in a ListView RRS feed

  • Question

  • Hi,

    We have a WPF Application that ran well in .NET 3.5 built under VS2008 that was recently ported to VS2015 .NET 4.5 and is having problems.

    One of our ListView controls developed the following exception when trying to set an item to selected within the list control on an events.  I looked at the keys in each item and they are unique.

    The exception occurs at the line item.IsSelected = true;  Should I maybe be doing this differently under 4.5?

    System.ArgumentException was unhandled by user code
      HResult=-2147024809
      Message=An item with the same key has already been added.
      Source=mscorlib
      StackTrace:
           at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
           at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
           at System.Collections.Generic.Dictionary`2..ctor(IDictionary`2 dictionary, IEqualityComparer`1 comparer)
           at System.Windows.Controls.Primitives.Selector.InternalSelectedItemsStorage..ctor(InternalSelectedItemsStorage collection, IEqualityComparer`1 equalityComparer)
           at System.Windows.Controls.Primitives.Selector.SelectionChanger.ApplyCanSelectMultiple()
           at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
           at System.Windows.Controls.Primitives.Selector.SetSelectedHelper(Object item, FrameworkElement UI, Boolean selected)
           at System.Windows.Controls.Primitives.Selector.NotifyIsSelectedChanged(FrameworkElement container, Boolean selected, RoutedEventArgs e)
           at System.Windows.Controls.Primitives.Selector.OnSelected(Object sender, RoutedEventArgs e)
           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.RaiseEvent(RoutedEventArgs e)
           at System.Windows.Controls.ListBoxItem.OnSelected(RoutedEventArgs e)
           at System.Windows.Controls.ListBoxItem.OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
           at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
           at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
           at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
           at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
           at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
           at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
           at System.Windows.Controls.ListBoxItem.set_IsSelected(Boolean value)
           at SGVManagerSkin.Controls.CustomListView.ListViewItem_ButtonClick(Object sender, RoutedEventArgs e)
           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.RaiseEvent(RoutedEventArgs e)
           at System.Windows.Controls.Primitives.ButtonBase.OnClick()
           at System.Windows.Controls.Primitives.ToggleButton.OnClick()
           at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonDown(MouseButtonEventArgs e)
           at System.Windows.UIElement.OnMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e)
           at System.Windows.Input.MouseButtonEventArgs.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.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
           at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
           at System.Windows.Input.MouseButtonEventArgs.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.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
           at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
           at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
           at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
      InnerException:

           


    private void ListViewItem_ButtonClick(object sender, RoutedEventArgs e)
    {
        ListViewItem item = sender as ListViewItem;
        if (item != null)
        {
    
            if (this.SelectionMode != SelectionMode.Single)
            {
                SelectedItems.Clear();
            }
    
            item.IsSelected = true;
        }
    }




    • Edited by hc0656 Friday, September 22, 2017 3:08 PM
    Friday, September 22, 2017 3:05 PM

All replies

  • Hi,

    Sorry for late reply.

    >> The exception occurs at the line item.IsSelected = true;  Should I maybe be doing this differently under 4.5?

    There is no different between WPF 3.5 and WPF 4.5, check ListViewItem Class which is available since 3.0.

    According to the exception message:  An item with the same key has already been added

    Make sure that you are not adding several items with the same key to some dictionary, this error usually happens because of this.

    Check this thread: ListBox loses the ability to change selecteditem when selecteditem data is modifiedwhich indicate that you are changing the item's hashcode when you change it's ID.

    You can try this solution:

    • Remove the object to be changed from the list
    • Notify the UI of the change
    • Change the ID of your object (resulting in a change to it's hashcode)
    • Add your object back to the list and notify the UI of the change.

    And check :https://social.msdn.microsoft.com/Forums/vstudio/en-US/c27733eb-61ad-4f05-b7f8-502e70ee35e9/an-item-with-the-same-key-has-already-been-added?forum=wpf

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Bob Ding Wednesday, September 27, 2017 6:36 AM
    Monday, September 25, 2017 2:47 AM