Page 1 of 1

Silverlight/WPF - Sample Data with Blend

#1 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Posted 24 October 2010 - 07:14 AM

In a previous tutorial, I showed how to do a custom Item Template for a ListBox. However, one of the painstaking parts of that was having to create the class and the code to populate the ListBox with actual data. Also, even though I was populating the ListBox, I still simply saw blank controls during design-time.

This is where Sample Data comes in. Not only can you quickly create sample data for a control, you can drag and drop it onto the control, and the ItemTemplate will be created for you(which you can still edit). And the biggest plus to using Sample Data?....the data can be view at DESIGN-TIME.

So first, let's create a Silverlight project in Blend. I named mine SampleDataProject.

Attached Image

Next, we will put a ListBox on our page, and give it a Height of 200 and a Width of 250.


Now that we have the ListBox on the page, we can create our Sample Data. Click on the Data window, then on the "Create sample data" button, then choose "New Sample Data..."...

Attached Image

We are going to name it PersonDataSource.

Attached Image

A couple of things to note on this screen. You will see two radio buttons: Project, and This document. If you choose Project, it will make the sample data available to any control in the project. If you choose This document, then the sample data will only be available to this page.

You will also see a checkbox on this screen saying "Enable sample data when application is running". This allows you to use the sample data in design time, but use another data source during runtime. Uncheck this box if you want to use a different data source at runtime. Since we want our sample data for design time and runtime, we will leave it checked.

After creating the sample data, the Data window will now show our data in a treeview type layout. By default, it will add two properties for us.

Let's change the first property by double clicking it and giving it the name "FullName".

Let's change the second property to "Age". By default, this property is set to a boolean type. Since our age is now a boolean value, we need to change that to a numeric format. Click on the "Change property type" button on the far right of the property line. You will see a dropdown with differnt types. Choose "Number". We will leave it's length at 2.

Attached Image

Now that we have the properties setup, we need to add some actual data. Click on the "Edit sample values" button...

Attached Image

You will now see a window with a number of rows and data. At the bottom, you can determine how many rows you want to show. We are going to change this to 3. We are going to add some data to those rows, then click OK.

Attached Image

Now that we have our sample data setup, let's simply drag and drop the "Collection" from the Data window onto the ListBox. The ListBox will create it's own ItemTemplate to display the data.

That's all you have to do. As you can see, we now have data in our ListBox while designing it...

Attached Image

You can look at my other tutorial on how to edit the created ItemTemplate to show the data different ways.

To me, this is one of the best features of Blend. Being able to see how a collection control will look at design time will speed up the development process. No longer will you have to continuously run the application to see if you have the layout correct.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1