.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
abhays
Clintonzz
arronlee
mattyclown
cathyhill345

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Silverlight ObservableCollection

Posted By:Mahadesh Mahalingappa       Posted Date: August 09, 2011    Points: 200    Category: Silverlight    URL:   

In this article I would introduce you to ObservableCollection in Silverlight and how to use an Observable Collection.
 

In this article I introduce you to Observable Collections in Silverlight.

Creating tables:

create table Person 
(
id int identity primary key,
FirstName varchar(50),
LastName varchar(50)
)
insert into Person values ('Jill','Jack');

Data Model Wizard
The Data Model was created as shown below:

Data Model

Created a class Person as shown below:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string _firstname;

    public string FirstName
    {
        get { return _firstname; }

        set
        {
            _firstname = value;
            NotifyPropertyChanged("FirstName");
        }
    }


}


Note that the class implements the interface InotifyPropertyChanged. The Interface looks like below:

namespace System.ComponentModel
{
    // Summary:
    //  Notifies clients that a property value has changed.
    public interface INotifyPropertyChanged
    {
        // Summary:
        //  Occurs when a property value changes.
        event PropertyChangedEventHandler PropertyChanged;
    }
}

Creating Silverlight Enabled WCF Service.

Silverlight WCF Service

Added the method GetPersons:

[OperationContract]
public IEnumerable GetPersons()
{
    ProjectsDataEntities context = new ProjectsDataEntities();
    var resultset = from person in context.People
                    select new Persons
                    {
                        Firstname = person.FirstName
                    };
    return resultset;
} 

Created a Person class:

    public class Persons
    {
        public string Firstname { get; set; }
    } 


Run the service:

Run Silverlight WCF Service

Add the Service Reference.

Add Service Refernces to Silverlight WCF Services

Now let's go to the Mainpage.xaml.cs.

Adding the reference as shown below:

using ObservableSample.DataServiceReference;



public partial class MainPage : UserControl
    {
        ObservableCollection collection; 
        public MainPage()
        {
            InitializeComponent();
            collection = new ObservableCollection();

            DataServiceClient client = new DataServiceClient();
            client.GetPersonsCompleted += new EventHandler(client_GetPersonsCompleted);
            client.GetPersonsAsync(); 

        }


        void client_GetPersonsCompleted(object sender, GetPersonsCompletedEventArgs e)
        {
            collection = e.Result;

            Dispatcher.BeginInvoke(delegate()
            {
                collection[0].Firstname = "Gill";
            });

            dataGrid1.ItemsSource = collection;
        } 

    }

 In the client_GetPersonsCompleted, I pass the resultset from the WCF Service to the Collection. Modify the first element of the collection and then assign the collection to the ItemSource of the DataGrid.

Silverlight Observable Collection

In this example we have seen how to use an Observable Collection though I have not shown you the real advantage of Observable Collection. This was for the very beginners. I will in my next article explain how an Observable collection helps us in two-way binding i.e. Database to Silverlight and Silverlight to Database.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend