Use of Observablecollection in Silverlight
-
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?
-
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?
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) -
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)Can u please explain me how the changes to the _studentList can reflect to UI with code?
-
Can u please explain me how the changes to the _studentList can reflect to UI with code?
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) -
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)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.
-
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.
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) -
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.
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 implementINotifyPropertyChanged
in the class (or an ancestor of). Simplistically, when an item changes, thePropertyChanged
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
-
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 implementINotifyPropertyChanged
in the class (or an ancestor of). Simplistically, when an item changes, thePropertyChanged
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
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
-
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
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