Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Behind the scenes of the ObservableCollection(Of T)

When developing WPF applications, in many situations you will use the ObservableCollection(Of T) that supports the two ways data-binding and therefore it is able to notify the UI of changes happening on items of the collection itself. This is due to the implementation of the INotifyPropertyChanged interface.


It is interesting to understand how this works behind the scenes. For the sake of simplicity, for this blog post I created a Console application adding a reference to the WindowsBase.dll assembly that defines the above mentioned collection.


The ObservableCollection basically works like the List(Of T), meaning that it exposes methods such as Add, Remove and so on. Now let's imagine we want to build a simple collection of strings, as follows:


        'Requires an Imports System.Collections.ObjectModel directive

        Dim stringCollection As New ObservableCollection(Of String)


The collection exposes an event named CollectionChanged, which is raised each time items within the collection are edited (including add/remove operations). First of all, we have to specify an event handler that will be implemented later:


        AddHandler stringCollection.CollectionChanged,

                   AddressOf stringCollectionChangedEventHandler


Now we can perform some operations such as adding and removing items:


        stringCollection.Add("One string")

        stringCollection.Add("Two strings")

        stringCollection.Add("Three strings")


        stringCollection.Remove("Three strings")



The event handler adheres to the specific delegate's signature; the following is just an example:


    Private Sub stringCollectionChangedEventHandler(ByVal sender As Object,

                                                    ByVal e As Specialized.NotifyCollectionChangedEventArgs)

        Select Case e.Action

            Case Is = Specialized.NotifyCollectionChangedAction.Add

                Console.WriteLine("Added a new string")

            Case Is = Specialized.NotifyCollectionChangedAction.Remove

                Console.WriteLine("Removed a string")

        End Select

    End Sub


The System.Collections.Specialized.NotifyCollectionChangedEventArgs type allows getting information on what modifications affected the collection. For example, the Action property allows discovering what kind of action (add, remove, replace) affected the collection, while the NewItems and OldItems properties allow respectively getting information on what items were added and what items were removed/moved/replaced. 


Because of this mechanism, the two ways WPF data-binding can intercept the event and can communicate to the runtime that the UI needs to refresh according to the modifications.



Print | posted on martedì 1 settembre 2009 21:39 | Filed Under [ Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 5 and 4 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET