Creating Sample Data from a Class - Page 2

by kirupa  |  5 February 2011

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

In the previous page, you created a simple class and generated some sample data based on the properties that class exposed. In this page, let's go a step further and learn how to generate a collection so that you can see many instances of your simple class!

Defining a Collection of Sample Data Values
Right now, our Address class is pretty boring...and lonely. If you want to see a collection of Addresses, you'll need to define another class that closely mimics a collection.

Go back into Visual Studio, Reload the project if prompted, and make sure Address.cs is open and displayed. At the bottom of your code, outside of the Address class, let's define a new class called AddressList. Go ahead and add the AddressList code (the part below that isn't faded):

public class Address
{
public string Name
{
get;
set;
}
 
public string Street
{
get;
set;
}
 
public int ZipCode
{
get;
set;
}
}
 
public class AddressList : ObservableCollection<Address>
{
}

You may need to add a using statement to resolve your use of ObservableCollection, so go ahead and do that if you are getting build errors or warnings.

Before we jump back into Blend and marvel at our latest creation, let's just look at what this one line of code actually does. What you have done is defined a class that is nothing more than a collection of Address objects. More specifically, you created a new class called AddressList. This class extends the ObservableCollection class to give it magical item carrying powers, and you specify that your collection will only store Address objects.

Jump back into Blend, and make sure to Build your project again. It is important to build, for if you don't build your project again, the AddressList class you added in Visual Studio will be ignored by Blend. Blend is a bit high-maintenance like that. Anyhoo, go back to your Data panel, click on the Create Sample Data button, and click (one more time!) on the Create Sample Data from Class menu item.

The Create Sample Data from Class dialog will appear again, but this time, notice what you see extra:

[ the AddressList class you saw earlier is being displayed ]

The collection of addresses you dubbed AddressList is now visible. Double click on it (or single click and click the OK button) to add a new sample data source based on your AddressList class.

In your Data panel, you will now see your AddressList appear. Because AddressList is a collection that takes Address objects as its children, you'll see the three Address properties displayed beneath it:

[ you now see a collection of Addresses ]

If you drag and drop your AddressList onto your artboard to see the sample data generated, you will see a collection of sample data as opposed to just a single piece of sample data that you saw before!

Conclusion
The ability to generate sample data from a class that you define closes a missing part of the original sample data feature you may have used. By allowing you to define the structure of the sample data that gets generated, you retain the ability to design the data inside Expression Blend while avoiding the hassle of mapping between a datasource created by Blend and one that closely mimics your live data.

Need Help?

If you have questions, need some assistance on this topic, or just want to chat - please drop by our friendly forums and post your question. There are a lot of knowledgeable and witty people who would be happy to help you out. Plus, we have a large collection of smileys you can use

Share

Did you enjoy reading this and found it useful? If so, please share it with your friends:

If you didn't like it, I always like to hear how I can do better next time. Please feel free to contact me directly at kirupa[at]kirupa.com.

Cheers!

Kirupa Chinnathambi
about | facebook | twitter


 1 | 2

SUPPORTERS:

cloud storage
EdgeCast CDN
kirupa.com's fast and reliable hosting provided by Media Temple. Creative web apps. Make your own free flash banners and photo slideshows.
HTML5 CSS3 Mobile Gallery for iPhone, iPad Flash effects. Art without coding.
Flipping Book - page flip flash component. Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery
Learn how to advertise on kirupa.com
 
SHARE:



MINI SUPPORTERS: