Updating SelectedItems

2 posts, 0 answers
  1. Frustrated Dev
    Frustrated Dev avatar
    35 posts
    Member since:
    Oct 2006

    Posted 17 Apr 2010 Link to this post


    I have a GridView that contains a bunch of items. When a user selects an item, I have an event handler for the SelectionChanged event. In that event handler, I need to automatically select items that are similar to this item. The code that attempts this looks like the following:

    private void itemGridView_SelectionChanged(object sender, SelectionChangeEventArgs e) 
      itemGridView.SelectionChanged -= itemGridView_SelectionChanged; 
      List<MyItem> similarItems = GetSimilarItems(itemGridView); 
      foreach (MyItem similarItem in similarItems) 
      itemGridView.SelectionChanged += itemGridView_SelectionChanged; 

    The GetSimilarItems method programmatically identifies which items are similar. However, when I go to add them to the SelectedItems collection programmatically (as shown above), I receive an error. The error bubbles up through JavaScript and says:

    "Error: Unhandled Error in Silverlight Application
    Code: 4004    
    Category: ManagedRuntimeError       
    Message: System.InvalidOperationException: Cannot change ObservableCollection during a CollectionChanged or PropertyChanged event.
       at System.Collections.ObjectModel.ObservableCollection`1.CheckReentrancy()
       at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
       at System.Collections.ObjectModel.Collection`1.RemoveAt(Int32 index)
       at Telerik.Windows.Data.Selection.SelectionHandler.SynchronizeSelectedItems()
       at Telerik.Windows.Data.Selection.SelectionHandler.SynchronizePublicProperties()
       at Telerik.Windows.Data.Selection.SelectionHandler.EndSelection()
       at Telerik.Windows.Data.Selection.SelectionHandler.SelectedItems_CollectionChanged(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.Collections.ObjectModel.Collection`1.Add(T item)
       at MyApp.Pages.Home.itemGridView_SelectionChanged(Object sender, SelectionChangeEventArgs e)"

    How do I programmatically select some items when a user has selected an item?

    Thank you
  2. Milan
    Milan avatar
    1989 posts

    Posted 22 Apr 2010 Link to this post

    Hi Frustrated Dev,

    It is not possible to change a collection in its CollectionChanged event but you can workaround the problem by running your code through the Dispatcher, which will execute the selection change a bit later.

    Best wishes,
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft R3 2016 release webinar banner
Back to Top