MCCB ObservableCollection exceptions

2 posts, 0 answers
  1. Ian
    Ian avatar
    3 posts
    Member since:
    Dec 2012

    Posted 05 Feb 2019 Link to this post

    Hi, I am having an issue with a standalone RadMultiColumnComboBox.  It needs to be populated differently each time I select a different entity (User) from a RadGridView.  As I understand it the only way to do this is with events. 

    The problem is that I run into an exception when I try to clear the selected items from the MCCB (basically, I need to reset it), and add the new selections.  I get various ObservableCollection changed event exceptions.  

    What is the correct way to do this to avoid these type of exceptions? 




    Here is the code I use so far: 


    Private changingUser As Boolean = False
    Private Sub RadMultiColumnComboBox_SelectionChanged(sender As Object, e As SelectionChangeEventArgs)
            SyncLock lockObject
                If changingUser Then
                    e.Handled = True
                    changingUser = False
                    Exit Sub
                End If
            End SyncLock
            If e.AddedItems.Count = 0 AndAlso e.RemovedItems.Count = 0 Then Exit Sub
            vm.AddRemoveRoles(e.AddedItems, e.RemovedItems)
        Catch ex As Exception
            e.Handled = True
        End Try
    End Sub
    Private Sub Grid_SelectionChanged(sender As Object, e As SelectionChangeEventArgs)
            SyncLock lockObject
                changingUser = True
            End SyncLock
            RolesBox.SelectedItems.SuspendNotifications() 'RolesBox is the RadMultiColumnComboBox
            Dim roles = vm.GetSelectedUserRoles
            For Each r In roles
        Catch ex As Exception
            SyncLock lockObject
                changingUser = False
            End SyncLock
        End Try
    End Sub


    Here is an example of the exceptions I am getting: 

    System.InvalidOperationException: 'Cannot change ObservableCollection during a CollectionChanged event.'

       at System.Collections.ObjectModel.ObservableCollection`1.CheckReentrancy()
       at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
       at System.Collections.ObjectModel.Collection`1.Remove(T item)
       at Telerik.Windows.Controls.RadMultiColumnComboBox.OnSelectedItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
       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 Telerik.Windows.Controls.MultiColumnComboBox.SelectionBridge.ItemsSelectedInOwner(IEnumerable`1 addedItems)
       at Telerik.Windows.Controls.MultiColumnComboBox.SelectionBridge.OwnerSelectedItemsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
       at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)

  2. Dinko
    Dinko avatar
    1111 posts

    Posted 07 Feb 2019 Link to this post

    Hello Ian,

    Thank you for the provided code snippets. 

    If I have understood your scenario correctly, you have separate RadGridView and RadMultiColumnComboBox and you are trying to sync their selected items. I have double check this on my side but wasn't able to reproduce this exception. The project is attached to this reply. May I ask you to take a closer look and let me what I need to modify to reproduce this exception.

    Progress TelerikWant to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top