English 中文(简体)
Handling ObservableCollection CollectionChanged Event
原标题:

I ve seen code like the following in the Silverlight toolkit and can t understand how it is safe to do:

private void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // Update the cache
    if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems != null)
    {
        for (int index = 0; index < e.OldItems.Count; index++)
        {
            _items.RemoveAt(e.OldStartingIndex);
        }
    }
}

If you remove an item from say index 5 doesn t that change the current index of every item in the _items collection after index 5 to be one less than before? So then how is it safe to continually remove items using their "old" indexes like this code does?

I d really like to understand why this works.

Any ideas?

问题回答

This code looks like it is removing a contiguous set of items from a particular starting index. If you read the remove call carefully:

_items.RemoveAt( e.OldStartingIndex );  

you will notice that it is iteratively removing items from a constant index. This means it is collapsing the list by removing items in a contiguous range. This may very well be correct - assuming that is the intent of the code.

The loop executes as many times as e.OldItems.Count indicates. So (presumably) it is told how many items to remove starting from a given index.

As a general practice, you have to be careful about how you remove items from collections for a couple reasons:

  1. Item indexes do indeed change as you remove items. So care must be taken to avoid errors resulting from item index positions shifting.
  2. Mutating a list while iterating over it (as in a foreach loop or with an explicit IEnumerator) results in exceptions. You cannot mutate a list (either add or delete) while iterating - it invalidates the iterator.




相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签