Page view counter

Tip of the Day - Binding to a Collection

Source Code - Same as Yesterday 

 

The Tip of the Day doesn't usually carry on a related series of postings, but data binding is so important that I'm making an exception this weekend. Yesterday we looked at laying out a form using a grid holding a DataGrid and a second grid to create a master detail relationship, with the end goal looking like this:

MasterDetail

To populate the DataGrid that we've placed in the left column of the outer grid, we'll need a collection of business objects. To keep this simple, I've returned to the idea of a book class,

BookClass

While it isn't strictly necessary for my Book class to implement INotifyPropertyChanged, it is convenient to have it do so, so that if the Book object is changed in the underlying data, the UI can be updated as well. Thus, each of the underlying private member fields  has a public property whose getter returns the private field and whose setter both sets the field and calls the Notify method to update the UI,

BookProperties

With the book class in place, it is easy to create a collection of Book objects based on the last few books I bought for my Kindle.  The steps are:

  1. Create a member variable to hold the collection
  2. Allocate memory for the collection
  3. Add objects to the collection
  4. Set the DataGrid to bind to properties of each individual object
  5. Set the collection as the ItemSource property of the DataGrid

Create a member variable to hold the collection

AllocateList

Allocate memory for the collection

AllocateMemory

Add objects to the collection

You can imagine that we would normally be retrieving these objects from a database, web server, or other common data source. Here we are hand coding to keep life simple.

AddBook

AddBookImplemented

Set the DataGrid to bind to properties of each individual object

DataGrid

In this small snippet you can see that we've created an instance of the DataGrid, named it Books, placed it in the grid, and told it not to auto generate its columns (we only want two columns, not all of the properties of the Book!).  We then manually create two text box columns, one for the Title and one for the ISBN.

Set the collection as the ItemSource property of the DataGrid

ItemSource

If you look closely at the line numbers you'll see that I cut out most of the "Add Book" calls to focus on the call to setting the ItemsSource on the DataGrid (look at line 23 in the previous image!).  By creating this binding, when the page loads the collection is bound to the data grid and the two properties from each book object are displayed automagically,

DataGridDisplayed

More soon on displaying the details and then moving beyond!

Published Sunday, March 16, 2008 10:00 AM by jesseliberty
Filed under:

Comments

# re: Tip of the Day - Binding to a Collection

Nice tip, I would like to see a CRUD implementation using a database.

Sunday, March 16, 2008 11:38 AM by anuomotayo

# re: Tip of the Day - Binding to a Collection

Thanks Jesse, I would also like to see this using a database.

Sunday, March 16, 2008 2:13 PM by MoHassan

# re: Tip of the Day - Binding to a Collection

Michael just posted an article on Silverlight and ADO.NET:

michaelsync.net/.../adonet-data-service-astoria-in-silverlight-2-beta1

Sunday, March 16, 2008 2:28 PM by wisecarver

# Silverlight Cream for March 16, 2008 -- #226

Jose Fajardo has a Control Contracts Diagram, Jesse Liberty binds a DataGrid to a collection, Andy Beaulieu

Sunday, March 16, 2008 5:39 PM by Community Blogs

# re: Tip of the Day - Binding to a Collection

Thank you! This helps a lot.

Monday, March 17, 2008 7:51 AM by zhangyang0803

# Dew Drop - March 17, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - March 17, 2008 | Alvin Ashcraft's Morning Dew

# re: Tip of the Day - Binding to a Collection

Yes, ...the BIG WHITE ELEPHANT in the room since Mix08...How do you update a database???

Everyone is showing us how to PULL data, No one is showing us how to PUSH!

(in the link above,) Michael requires us to download a small battalion of addons/updates/CTPs to be able to use CRUD.  It's got to be easier than that, no?

Am I missing something? How come no one is talking about the most fundamental aspects of building an app: saving back to the database using Silverlight2.  (Go ahead, try searching for it on the net.)

Any small sample would be so very appreciated!! I love Silverlight2, and would really love to get going and building some real world apps.

Thanks.

Monday, March 17, 2008 8:47 PM by FlyingFishStix

# re: Tip of the Day - Binding to a Collection

Okay, no problem, I'll post on 2 way binding and then make sure my next couple videos are on one and two way binding. I'll also look at where to put in binding to a sql db rather than to a biz object.  

You'll also find 2 way binding in the tutorial on data binding already posted in silverlight.net/learn/tutorials.aspx.

Tuesday, March 18, 2008 10:58 AM by jesseliberty

# re: Tip of the Day - Binding to a Collection

Thanks Jesse for going ahead and making some videos to explain SL2 + WCF-services(?) that can update a back-end SQL server DB.  Your tutorials and how-to videos are great, and we really appreciate all your time in doing those.  (Those 10-15min how-to videos are worth their weight in gold.)

If you're about to do those videos then, may I make a request?  If the world is moving to "software as a service" and since so many of us have built tons of live web/wcf services, it would be a shame not to use them.  If you could make a video tying Silverlight2 + WCF Services + Datasets(?) + SQL Server, that would be great!  (Please nothing with a direct link to a database since the world is moving away from that.)

We simply want to take the great UI (and great x-browser compatibility) of Silverlight and tie it in with existing services.  In other words, the CRUD and Merge operations we can do with ADO.NET and WCF , but using a Silverlight2 UI.  We don't want to reinvent the wheel, we just want to use all the great UX & developer/deployment benefits of a Silverlight Front-end, but keeping the back-end services intact.

One thing to note: It appears that Silverlight2 does NOT include the Dataset stack (which still has me scratching my head...isn't that what Microsoft has been telling us to use all these years?).  So I'm not sure what you/we are up against if a WCF Service passes over a Dataset.  If you know the reasoning why Datasets were left out, please explain it on the video.

While we wait for the video, here's a link to a Silverlight Tutorial that talks about working with a WCF proxy.  This link might help others too that are in the same boat (although, nothing beats a how-to video with downloadable source) >>

silverlight.net/.../323aac2f-14cc-4dd8-bb40-fa2cb3c2e522.aspx

Thanks again Jesse, you're doing a great service for the community!

Cheers mate!

-Mike

Wednesday, March 19, 2008 11:49 AM by FlyingFishStix

# re: Tip of the Day - Binding to a Collection

I have written a 2-way data binding example that uses Linq to persist on the server side

Enjoy

Sunday, May 04, 2008 5:57 PM by jmcfet

# re: Tip of the Day - Binding to a Collection

Pingback from  re: Tip of the Day - Binding to a Collection

Sunday, May 04, 2008 7:03 PM by re: Tip of the Day - Binding to a Collection