This is a migrated thread and some comments may be shown as answers.

Updating SelectedItems

1 Answer 92 Views
This is a migrated thread and some comments may be shown as answers.
Frustrated Dev
Top achievements
Rank 1
Frustrated Dev asked on 17 Apr 2010, 02:31 PM

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

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 22 Apr 2010, 11:54 AM
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.
Asked by
Frustrated Dev
Top achievements
Rank 1
Answers by
Telerik team
Share this question