Click here to Skip to main content
15,893,814 members
Articles / Web Development / ASP.NET

List vs ObservableCollection vs INotifyPropertyChanged in Silverlight

,
Rate me:
Please Sign up or sign in to vote.
4.60/5 (90 votes)
21 Sep 2009CC (ASA 2.5)2 min read 368.4K   5.7K   110  
This article gives a basic understanding of List, ObservableCollection, and INotifyPropertyChanged.
  • sllistvsobservablecollection.zip
    • SLListVsObservableCollection
      • SLListVsObservableCollection.gpState
      • SLListVsObservableCollection.sln
      • SLListVsObservableCollection.suo
      • SLListVsObservableCollection.Web
      • SLListVsObservableCollection
        • App.xaml
        • App.xaml.cs
        • Bin
          • Debug
            • AppManifest.xaml
            • de
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
            • es
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
            • fr
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
            • it
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
            • ja
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
            • ko
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
            • Microsoft.Windows.Controls.DataVisualization.dll
            • SLListVsObservableCollection.dll
            • SLListVsObservableCollection.xap
            • System.ComponentModel.DataAnnotations.dll
            • System.ComponentModel.DataAnnotations.xml
            • System.Windows.Controls.Data.dll
            • System.Windows.Controls.Data.Input.dll
            • System.Windows.Controls.Data.Input.xml
            • System.Windows.Controls.Data.xml
            • System.Windows.Data.dll
            • System.Windows.Data.xml
            • TestPage.html
            • zh-Hans
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
            • zh-Hant
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Data.resources.dll
              • System.Windows.Data.resources.dll
        • Page.xaml
        • Page.xaml.cs
        • Properties
        • SLListVsObservableCollection.csproj
        • SLListVsObservableCollection.csproj.user
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Threading;
using Microsoft.Windows.Controls.DataVisualization.Charting;
using System.ComponentModel;

namespace SLListVsObservableCollection
{
    public partial class Page : UserControl
    {
        List<UserList> usersList;
        ObservableCollection<UserOC> usersOC;
        List<UserNPC> usersNPC;
        Timer timerList;
        Timer timerOC;
        Timer timerNPC;

        public Page()
        {
            InitializeComponent();
            usersList = getUserCollectionList();
            usersOC = getUserCollectionOC();
            usersNPC = getUserCollectionNPC();

            this.dgList.ItemsSource = usersList;
            this.dgOC.ItemsSource = usersOC;
            this.dgNPC.ItemsSource = usersNPC;

            ((DynamicSingleSeriesWithAxes)this.cList.Series[0]).ItemsSource = usersList;
            ((DynamicSingleSeriesWithAxes)this.cOC.Series[0]).ItemsSource = usersOC;
            ((DynamicSingleSeriesWithAxes)this.cNPC.Series[0]).ItemsSource = usersNPC;

            timerList = new Timer(timerFiredList, null, 5000, 1000);
            timerOC = new Timer(timerFiredOC, null, 5000, 1000);
            timerNPC = new Timer(timerFiredNPC, null, 5000, 1000);

        }

        void timerFiredList(object state)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                Random rnd = new Random();
                usersList[rnd.Next(0, usersList.Count)].Grade = rnd.Next(1, 5);

                if (rnd.Next(1, 5) > 3)
                {
                    if (usersList.Count > 4)
                    {
                        usersList.RemoveAt(4);
                    }
                    else
                    {
                        usersList.Add(new UserList { Name = "E", Grade = 1 });
                    }
                }
            });
        }
        void timerFiredOC(object state)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                Random rnd = new Random();
                usersOC[rnd.Next(0, usersOC.Count)].Grade = rnd.Next(1, 5);

                if (rnd.Next(1, 5) > 3)
                {
                    if (usersOC.Count > 4)
                    {
                        usersOC.RemoveAt(4);
                    }
                    else
                    {
                        usersOC.Add(new UserOC { Name = "E", Grade = 1 });
                    }
                }
            });
        }
        void timerFiredNPC(object state)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                Random rnd = new Random();
                usersNPC[rnd.Next(0, usersNPC.Count)].Grade = rnd.Next(1, 5);

                if (rnd.Next(1, 5) > 3)
                {
                    if (usersNPC.Count > 4)
                    {
                        usersNPC.RemoveAt(4);
                    }
                    else
                    {
                        usersNPC.Add(new UserNPC { Name = "E", Grade = 1 });
                    }
                }
            });
        }

        List<UserList> getUserCollectionList()
        {
            List<UserList> rVal = new List<UserList>();

            rVal.Add(new UserList { Name = "A", Grade = 2 });
            rVal.Add(new UserList { Name = "B", Grade = 3 });
            rVal.Add(new UserList { Name = "C", Grade = 4 });
            rVal.Add(new UserList { Name = "D", Grade = 5 });
            rVal.Add(new UserList { Name = "E", Grade = 1 });

            return rVal;
        }
        ObservableCollection<UserOC> getUserCollectionOC()
        {
            ObservableCollection<UserOC> rVal = new ObservableCollection<UserOC>();

            rVal.Add(new UserOC { Name = "A", Grade = 2 });
            rVal.Add(new UserOC { Name = "B", Grade = 3 });
            rVal.Add(new UserOC { Name = "C", Grade = 4 });
            rVal.Add(new UserOC { Name = "D", Grade = 5 });
            rVal.Add(new UserOC { Name = "E", Grade = 1 });

            return rVal;
        }
        List<UserNPC> getUserCollectionNPC()
        {
            List<UserNPC> rVal = new List<UserNPC>();

            rVal.Add(new UserNPC { Name = "A", Grade = 2 });
            rVal.Add(new UserNPC { Name = "B", Grade = 3 });
            rVal.Add(new UserNPC { Name = "C", Grade = 4 });
            rVal.Add(new UserNPC { Name = "D", Grade = 5 });
            rVal.Add(new UserNPC { Name = "E", Grade = 1 });

            return rVal;
        }
    }

    public class UserList
    {
        public string Name { get; set; }
        public int Grade { get; set; }
    }
    public class UserOC
    {
        public string Name { get; set; }
        public int Grade { get; set; }
    }

    public class UserNPC:INotifyPropertyChanged
    {
        private string name;
        public string Name { 
            get { return name; } 
            set { name = value; onPropertyChanged(this, "Name"); } 
        }

        public int grade;
        public int Grade { 
            get { return grade; } 
            set { grade = value; onPropertyChanged(this, "Grade"); } 
        }

        #region INotifyPropertyChanged Members
        // Declare the PropertyChanged event
        public event PropertyChangedEventHandler PropertyChanged;

        // OnPropertyChanged will raise the PropertyChanged event passing the
        // source property that is being updated.
        private void onPropertyChanged(object sender, string propertyName)
        {

            if (this.PropertyChanged != null)
            {
                PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Technical Lead Infosys
India India
Working as a Technology Lead in Infosys at Chennai, India.

Written By
Software Developer iSOFT
India India
Working as a Software Engineer in iSOFT at Chennai, India.

Comments and Discussions