Creating Sample Data from a Class - Page 1

by kirupa  |  5 February 2011

  Have questions? Discuss this Windows Phone tutorial with others on the forums.

One of the cool features in Expression Blend for making it easy to design your Windows Phone, Silverlight, and WPF applications is Sample Data. The way it works is very simple. You tell Blend to generate a schema for your sample data and, after a few mouse clicks, sample values are generated for you:

[ look ma, sample data! ]

While this is great if you just want to visualize some data for design purposes, it is not so great if you need to eventually tie it up with real data. The reason it isn't ideal is that your sample data is probably in a form that your live data may not be in, and trying to reconcile the automatically generated sample data with its own schema with your own datasource isn't fun.

To help avoid having you reconcile the sample data schema with your own data, Blend provides you with another way of generating the sample data. Instead of having Blend create both the sample data schema and generate the values for you, you specify the sample data schema yourself. By doing this, you control what your sample data looks like - more than likely modeling it after real data, but you let Blend continue to generate the sample values just as you had before.

This mystical feature that allows you to do this is called Create Sample Data from Class. In this article, you will learn how to use Expression Blend and Visual Studio to generate sample data based on a schema/model that you create yourself.

Creating the Class
The model for your sample data will just be a class that contains some public properties. It is these properties that will be used to generate your sample data.

As you can guess, you'll need to create a class first. To get started, create a new Windows Phone, WPF, or Silverlight project using Expression Blend. Once you have created your project, open this exact same solution in Visual Studio. You can do that easily by just right clicking on your solution via the Projects Panel and selecting Edit in Visual Studio:

[ easily edit your solution in Visual Studio ]

A few moments later, Visual Studio will launch with the exact same Solution opened and proudly displayed:

[ the same solution is now visible in Visual Studio ]

For things that involve code, such as adding and modifying a Class, I tend to rely on Visual Studio far more than Blend. To add a new Class file to our project, right click on your project (SampleDataFromClass in my screenshot) and go to Add | New Item.

The Add New Item dialog will appear. In this dialog, first select the Class item type:

[ you want to create a new item of type Class ]

Before you dismiss this dialog, give your Class a name as well. Find the Name field towards the bottom of this dialog and enter the name Address:

[ give your Class the name Address ]

After you have entered Address for your soon to be created Class, hit the Add button to dismiss this dialog and to create your Address.cs Class file. This will have automatically been opened for you:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace SampleDataFromClass
{
public class Address
{
 
}
}

Let's add some properties! Inside your constructor, add the following lines of code:

public class Address
{
public string Name
{
get;
set;
}
 
public string Street
{
get;
set;
}
 
public int ZipCode
{
get;
set;
}
}

You added three properties called Name, Street, and ZipCode. Notice the type of these three properties. The Name and Street properties are strings, and the ZipCode property is an int.

Generating the Sample Data
At this point, you have added a new class file and filled it in with three properties! That's good for now. Jump back into Expression Blend. You will probably see a dialog letting you know that your current project has been updated and changed. Click Yes to Reload the project, for the change it is notifying you about is from you having added Address.cs in Visual Studio a few seconds earlier.

If you look in your Projects Panel inside Expression Blend, you'll see Address.cs appear as well:

[ look, still the same solution! ]

Anyway, let's put our newly created class to good use. First, build your project via Project | Build or by pressing Ctrl + Shift + B. Now, jump over to your Data panel.

Click on the Create Sample Data button and, from the menu that appears, select Create Sample Data from Class:

[ it's time to create some Sample Data! ]

The Create Sample Data from Class dialog will appear. From this dialog, you will get to name your data source as well as select which class you want to base your sample data on:

[ just a dialog - the most important one for now! ]

Leave the data source name as is (unless you really want to rename it), but most importantly, select the Address class and press OK. When you press OK, this dialog will disappear and you will see your newly created data source appear in the Data panel. If you expand it, you will see your Address class and the Name, Street,and ZipCode properties:

[ your Class and its properties can be seen! ]

Woohoo! You can now use these data values just like how you learned in the earlier Introduction to Sample Data tutorial.

Something may look odd to you though. If you are a typical sample data user, you are probably familiar with using a collection of them as opposed to using just one like you are right now. That is because our data source is currently just a single class instance. To create a collection, we will have to modify our data source to point to a collection of Address objects instead. Let's do that...on the next page!

Onwards to the next page!


 1 | 2




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.