Creating a Observable Collection in Silverlight

Observable Collection :  Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

Let see a simple use of the Observable Collection .

public class ItemSourceCollection : ObservableCollection<ItemSource>
    {
        /// <summary>
        /// Add a <see cref="ItemSource"/> to the collection which specified by
        /// the picture URI and title.
        /// </summary>
        /// <param name="uri">The URI that references the source graphics file for the image.  </param>
        /// <param name="title">The string title for the picture.</param>
        public virtual void Add(Uri uri, string title)
        {
            ItemSource itemSource = new ItemSource(uri, title);
            this.Add(itemSource);
        }

        /// <summary>
        /// Remove all the item whose title match the
        /// supplied <paramref name="title"/>.
        /// </summary>
        /// <param name="title">The title of the item which will be removed.</param>
        /// <returns>The total item has been removed.</returns>
        public virtual int Remove(string title)
        {
            int count = 0;
            var matched = this.Items.Where(s => s.Title == title).ToList();
            foreach (var item in matched)
            {
                if (this.Remove(item))
                    count++;
            }
            return count;
        }
    }
Ebook Download
View all
Learn
View all