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.

Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out!

When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles.

Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.

COMMENTS


 1 | 2




SUPPORTERS:

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