Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Use of Observablecollection in Silverlight

Use of Observablecollection in Silverlight

Scheduled Pinned Locked Moved WPF
questionvisual-studiowpfdesignregex
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 4547220
    wrote on last edited by
    #1

    Hi, I have Question about the use of Observable Collection? During reading about MVVM Pattern i read about the observable collection that it reflect the changes to the UI Automatically which is not possible in the List collection.But i really dont understand it what it exactly means in practical terms. I want a demo for the Difference between the List VS ObservableCollection Vs PropertyChangedEventHandler. I have read the document on your site but i am yet not clear about it? will you please help me?

    I 1 Reply Last reply
    0
    • U User 4547220

      Hi, I have Question about the use of Observable Collection? During reading about MVVM Pattern i read about the observable collection that it reflect the changes to the UI Automatically which is not possible in the List collection.But i really dont understand it what it exactly means in practical terms. I want a demo for the Difference between the List VS ObservableCollection Vs PropertyChangedEventHandler. I have read the document on your site but i am yet not clear about it? will you please help me?

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      The ObservableCollection implements the INotifyCollectionChanged interface, so it fires a CollectionChanged event whenever an element is added, removed, replaced, or moved. The binding system knows how to react to objects that implement INotifyCollectionChanged and INotifyPropertyChanged... So when you bind to one of those, the binding system hooks the event and updates itself when it gets notified of a change. The List class doesn't implement that interface.

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      U 1 Reply Last reply
      0
      • I Ian Shlasko

        The ObservableCollection implements the INotifyCollectionChanged interface, so it fires a CollectionChanged event whenever an element is added, removed, replaced, or moved. The binding system knows how to react to objects that implement INotifyCollectionChanged and INotifyPropertyChanged... So when you bind to one of those, the binding system hooks the event and updates itself when it gets notified of a change. The List class doesn't implement that interface.

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        U Offline
        U Offline
        User 4547220
        wrote on last edited by
        #3

        Can u please explain me how the changes to the _studentList can reflect to UI with code?

        I 1 Reply Last reply
        0
        • U User 4547220

          Can u please explain me how the changes to the _studentList can reflect to UI with code?

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          I just explained it. The ObservableCollection fires a CollectionChanged event. The data binding system handles that event and triggers an update. That's it.

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          U 1 Reply Last reply
          0
          • I Ian Shlasko

            I just explained it. The ObservableCollection fires a CollectionChanged event. The data binding system handles that event and triggers an update. That's it.

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            U Offline
            U Offline
            User 4547220
            wrote on last edited by
            #5

            Sorry, but i am not getting what exactly i want. Actually I am new to silverlight so i am not clear with it. In the Given code (Populating a DataGrid in a Silverlight Application using MVVM) it is not calling the PropertyChanged Event. Can u provide code which calls that event.

            I P 2 Replies Last reply
            0
            • U User 4547220

              Sorry, but i am not getting what exactly i want. Actually I am new to silverlight so i am not clear with it. In the Given code (Populating a DataGrid in a Silverlight Application using MVVM) it is not calling the PropertyChanged Event. Can u provide code which calls that event.

              I Offline
              I Offline
              Ian Shlasko
              wrote on last edited by
              #6

              CollectionChanged, not PropertyChanged... The ObservableCollection class fires the CollectionChanged event internally, whenever you add/remove/replace/move items. The data binding system handles this event. All of this happens inside the framework... You won't see the code for it... It's automatic.

              Proud to have finally moved to the A-Ark. Which one are you in?
              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

              1 Reply Last reply
              0
              • U User 4547220

                Sorry, but i am not getting what exactly i want. Actually I am new to silverlight so i am not clear with it. In the Given code (Populating a DataGrid in a Silverlight Application using MVVM) it is not calling the PropertyChanged Event. Can u provide code which calls that event.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                There are two parts to binding that you are concerned with here. The first, as you are aware, is the use of ObservableCollection. This tells you that the collection has changed; in other words that items have been added or deleted in the collection. This does not tell you that an individual item has changed. To capture notifications that an individual item has changed, you need to implement INotifyPropertyChanged in the class (or an ancestor of). Simplistically, when an item changes, the PropertyChanged event fires, and this tells the binding engine that an item needs to be updated in the UI. If you look at samples, you'll typically see a class called something like ViewModelBase. This class is the one that implements the property changed notification, saving you from having to update the code in lots and lots of locations. Here's a typical example of this class:

                public abstract class ViewModelBase : INotifyPropertyChanged
                {
                public event PropertyChangedEventHandler PropertyChanged;
                /// <summary>
                /// Raised because the property has changed.
                /// </summary>
                /// <param name="property">The name of the property that changed.</param>
                protected virtual void OnChanged(string property)
                {
                var handler = PropertyChanged;
                if (handler == null)
                {
                return;
                }

                  handler(this, new PropertyChangedEventArgs(property));
                }
                

                }

                Now, you'll derive from this class and call OnChanged in your property setters. This would typically look something like this:

                private string _passCode;
                public string PassCode
                {
                get { return _passCode; }
                set
                {
                if (_passCode == value) return;
                _passCode = value;
                OnChanged("PassCode");
                }
                }

                When the user changes a value in PassCode, the value is updated and then the change notification is raised which tells the binder to update only that named field (if you want to update all fields, you pass an empty string to OnChanged). There are a couple of things to note here - always test to see if the update value is the same as the value you already store, this prevents you updating something that hasn't changed; the name of the property that is raised must match the name of the property, so calling "Pa4sCode" would fail to update the value as it's not the name of the property (PassCode). I hope that this makes sense to you. All too often people fall into the trap of assuming that ObservableCollect

                U 1 Reply Last reply
                0
                • P Pete OHanlon

                  There are two parts to binding that you are concerned with here. The first, as you are aware, is the use of ObservableCollection. This tells you that the collection has changed; in other words that items have been added or deleted in the collection. This does not tell you that an individual item has changed. To capture notifications that an individual item has changed, you need to implement INotifyPropertyChanged in the class (or an ancestor of). Simplistically, when an item changes, the PropertyChanged event fires, and this tells the binding engine that an item needs to be updated in the UI. If you look at samples, you'll typically see a class called something like ViewModelBase. This class is the one that implements the property changed notification, saving you from having to update the code in lots and lots of locations. Here's a typical example of this class:

                  public abstract class ViewModelBase : INotifyPropertyChanged
                  {
                  public event PropertyChangedEventHandler PropertyChanged;
                  /// <summary>
                  /// Raised because the property has changed.
                  /// </summary>
                  /// <param name="property">The name of the property that changed.</param>
                  protected virtual void OnChanged(string property)
                  {
                  var handler = PropertyChanged;
                  if (handler == null)
                  {
                  return;
                  }

                    handler(this, new PropertyChangedEventArgs(property));
                  }
                  

                  }

                  Now, you'll derive from this class and call OnChanged in your property setters. This would typically look something like this:

                  private string _passCode;
                  public string PassCode
                  {
                  get { return _passCode; }
                  set
                  {
                  if (_passCode == value) return;
                  _passCode = value;
                  OnChanged("PassCode");
                  }
                  }

                  When the user changes a value in PassCode, the value is updated and then the change notification is raised which tells the binder to update only that named field (if you want to update all fields, you pass an empty string to OnChanged). There are a couple of things to note here - always test to see if the update value is the same as the value you already store, this prevents you updating something that hasn't changed; the name of the property that is raised must match the name of the property, so calling "Pa4sCode" would fail to update the value as it's not the name of the property (PassCode). I hope that this makes sense to you. All too often people fall into the trap of assuming that ObservableCollect

                  U Offline
                  U Offline
                  User 4547220
                  wrote on last edited by
                  #8

                  Hi, Thanks. I understaqnd it now. I am new to Silverlight. But i am working on the project which is based on silverlight. This project is completed but i am trying to understand this. Would you please suggest me some resources to learn silverlight as early as possible (Books ,Sights, etc.). Thanks Umesh Tayade

                  P 1 Reply Last reply
                  0
                  • U User 4547220

                    Hi, Thanks. I understaqnd it now. I am new to Silverlight. But i am working on the project which is based on silverlight. This project is completed but i am trying to understand this. Would you please suggest me some resources to learn silverlight as early as possible (Books ,Sights, etc.). Thanks Umesh Tayade

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    There are plenty of blogs about Silverlight, and several CPians maintain SL blogs. Probably the premier SL site though, is Jesse Liberty's site[^]. As far as books go, I'd recommend Laurent Bugnion's excellent Silverlight 4 Unleashed[^].

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups