This question is locked. New answers and comments are not allowed.
Hello,
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:
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
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.SelectedItems.Add(similarItem); |
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