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

ObservableItemCollection<T>

3 Answers 85 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Randy
Top achievements
Rank 1
Randy asked on 15 Sep 2012, 04:25 PM
Hi there,

I'm using your ObservableItemCollection<T> and I have two issues with it. (v. 2012.2.607.40)

First, there's no constructor that takes an IEnumerable<T> or List<T> like the System.Collections.ObjectModel version it eventually inherits from. Though not a requirement, this is very useful for initializing a collection before doing anything else to it.

Secondly (and most importantly) there doesn't appear to be support for adding null items. I get a NullReferenceException when calling:

collection.Add(null);

The code is probably connecting to the item's PropertyChanged event, but a simple check for null during add/remove would allow support for null items.

I've extended the class to support the constructors, but I don't know overloading OnCollectionChanged or some other method will solve my issue. If so, please let me know what this method should contain.

On a slightly different topic, are the Suspend/Resume Notification methods called automatically on AddRange, InsertRange, and RemoveRange? Or do they need to be explicitly called?

Thanks!

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 18 Sep 2012, 10:54 AM
Hello,

Here is how the method should be written if you want to have nulls as items:

       protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnCollectionChanged(e);


            if (e.NewItems != null)
            {
                foreach (INotifyPropertyChanged item in e.NewItems.OfType<object>().Where(i=>i != null))
                    item.PropertyChanged += this.OnItemPropertyChanged;
            }


            if (e.OldItems != null)
            {
                foreach (INotifyPropertyChanged item in e.OldItems.OfType<object>().Where(i => i != null))
                    item.PropertyChanged -= this.OnItemPropertyChanged;
            }
        } 

InsertRange and RemoveRange methods will automatically suspend and resume notifications (I've added them few months ago) while AddRange and will not since it was legacy and we didn't wanted breaking change. 

All the best,
Vlad
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Randy
Top achievements
Rank 1
answered on 18 Sep 2012, 11:01 AM
Great, thanks!

One more question: are suspend and resume calls reference counted?
Do I have to call resume once for each suspend, or will one trump the other?

Thanks again!

Randy
0
Rossen Hristov
Telerik team
answered on 18 Sep 2012, 11:04 AM
Hello,

They are not counted. Here is the source code:

/// <summary>
/// Suspends the notifications.
/// </summary>
public virtual void SuspendNotifications()
{
    this.notificationsSuspended = true;
}
 
/// <summary>
/// Resumes the notifications.
/// </summary>
public virtual void ResumeNotifications()
{
    this.notificationsSuspended = false;
 
    if (this.IsDirty)
    {
        this.IsDirty = false;
        this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}
 
bool notificationsSuspended;

I hope this helps.

Greetings,
Ross
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

Tags
General Discussions
Asked by
Randy
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Randy
Top achievements
Rank 1
Rossen Hristov
Telerik team
Share this question
or