Visual Studio 2010 Beta2 Sample Data Project Templates

Silverlight 3 application demonstrating Sample Data.

SilverlightSampleData

Displaying Sample Data in the WPF and Silverlight Designer for Visual Studio 2010 Beta2 is now very easy with the design time DesignData markup extension.

The below video is a complete tutorial for using Sample Data in your WPF and Silverlight applications.  Please take a view few minutes to view it.

The Sample Application templates have many comments in them, walking you through Sample Data.  Please take a few minutes and review the projects created by the Sample Application Templates.

Beta2 Template Information

For Beta2, you need to use these templates to consume sample data in your projects.  The reason is the winfx.targets file for was not updated with the Sample Data Build Actions in time for Beta2.

These project files have been modified to include the DesignData Build Action for the Sample Data files.  All Sample Data files must have their Build Action set to DesignData in the properties window.

These templates will not be required for the final version of Visual Studio 2010.

There are really two templates.

1.  <Language> <Platform> Empty Application – this template consists of an empty project file and the added Build Action DesignData.

2.  <Language> <Platform> Sample Application – this template consists of a sample application that has sample data files and a UserControl that demonstrates how to consume the sample data.  It also includes the added Build Action DesignData.

WPF Templates

VB WPF Application DesignData – Empty VB.NET WPF Application template with sample data support.

VB WPF Application DesignData Sample – VB.NET Sample WPF Application that demonstrates consuming sample data.

CS WPF Application DesignData – Empty C# WPF Application template with sample data support.

CS WPF Application DesignData Sample – C# Sample WPF Application that demonstrates consuming sample data.

Silverlight Templates

VB SL Application DesignData – Empty VB.NET SL Application template with sample data support.

VB SL Application DesignData Sample – VB.NET Sample SL Application that demonstrates consuming sample data.

CS SL Application DesignData – Empty C# SL Application template with sample data support.

CS SL Application DesignData Sample – C# Sample SL Application that demonstrates consuming sample data.

Sample Application Template Usage

The WPF and Silverlight sample application template creates an application complete with sample data files, entity classes, a UserControl consuming the sample data files and

xmlns:d – How To Easily Add The d: Namespace

All design time properties live in a design time namespace that is part of this schema: http://schemas.microsoft.com/expression/blend/2008.

Examples of design time properties are, d:DesignWidth, d:DesignHeight, d:DesignInstance, d:DesignData, d:DataContext, d:Source.

In order to use the d: properties you must have a xmlns declaration in your XAML.  In Visual Studio 2010 there is a very easy way to add it to a Window, UserControl or Page .xmal file.

If you select the root control, you’ll see a Root Size Mode button displayed in the lower right hand corner.  Clicking this button changes the size mode of your root container from Auto to Fixed or from Fixed to Auto.

If the d: namespace has not been added to the root control yet, clicking this button will do it for you.  In addition to the d: namespace, mc: is added and the mc:Ignorable is added also.

AutoSizedRoot

I have modified each of the starter templates below to automatically add this for you.  However, as you add new Windows, Pages or UserControls, those templates may not have the d: namespace declarations, so the above trick comes in handy.

Adding a New Sample Data File

Open an existing Sample Data project or Create a new project using one of the Sample Data templates.

Add a resource dictionary to the project naming it <my entity class name>SampleData.xaml.

For example:  Customer sample data file would be named, “CustomerSampleData.xaml”

The the file properties as pictured below:

SampleDataSettings

Build Action: DesignData

Copy to Output Directory: Do not copy

Custom Too:  <blank>

Editing a New Sample Data File

This is an example of a single instance sample data file.

<local:Customer
    xmlns:local="clr-namespace:WPFApplication2"
    Age="21" Email="jim@hotmail.com" 
    FirstName="Jim" LastName="Smith" />

Sample Data files allow you to construct your objects in XAML.  You can set the required properties as illustrated above.

The below code illustrates how to create a collection of items in a sample data file.

<local:Customers xmlns:local="clr-namespace:WPFApplication2">
    <local:Customer 
        Age="21" Email="jim@hotmail.com" 
        FirstName="Jim" LastName="Smith" />
    <local:Customer 
        Age="22" Email="jane@hotmail.com" 
        FirstName="Jane" LastName="Smith" />
</local:Customers>

The above Customers class derives from the Generic List of Customer.

Consuming Sample Data in WPF

<!-- DataGrid Sample-->
<Grid d:DataContext="{d:DesignData Source=/SampleData/PeopleSampleData.xaml}" 
      Grid.Row="1" Margin="7">
    <DataGrid ItemsSource="{Binding}"/>
</Grid>

Sample Data is exposed through the d:DataContext design time property.  Design time properties are not compiled into your applications.

In WPF this is how you start at the root, navigate to SampleData folder and get the xaml file.

Consuming Sample Data in Silverlight

<!-- DataGrid Sample-->
<Grid d:DataContext="{d:DesignData Source=../SampleData/PeopleCollectionSampleData.xaml}" 
      Grid.Row="1" Margin="7">
    <data:DataGrid ItemsSource="{Binding}"/>
</Grid>

In Silverlight you have to ../ up to the root, then navigate to SampleData folder and get the xaml file.

Slight difference between WPF and Silverlight.

Video

PLEASE view this short tutorial video.  You will get a full and quick understanding of this great feature.

This video link supports right click, save as…

  Sample Data Tutorial Video (22 minutes)

Installing Templates Using The Download

For VB.NET copy the download to:

C:\users\<your user name>\Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual Basic

Unzip the download in this folder.  You should have 4 zip files in the folder.  You are ready to go.

For C# copy the download to:

C:\users\<your user name>\Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual C#

Unzip the download in this folder.  You should have 4 zip files in the folder.  You are ready to go.

Downloads

After downloading one or both template downloads, you’ll need to rename the file extension from .doc to .zip.  This is a requirement of WordPress.com.

Requires:  Visual Studio 2010 Beta2

Silverlight Templates require:  Silverlight 3

VB.NET Project Templates (61KB) (updated 10-28-2009)

C# Project Templates (58KB) (updated 10-28-2009)

Installing Templates From Within Visual Studio 2010 Beta2

The sample data templates are also be individually available on the Visual Studio Code Gallery.

To use a single template open the New Project Dialog and select the Online Templates tab at the bottom left.  You can browse all available templates or search for “design” and all the templates will be listed.

Close

Hope you use and enjoy Sample Data in Visual Studio 2010 Beta2.

Gentle reminder, please watch the above video too.

Have a great day,

Just a grain of sand on the worlds beaches.

12 Responses to Visual Studio 2010 Beta2 Sample Data Project Templates

  1. [...] Visual Studio 2010 Beta2 Sample Data Project Templates « Karl On WPF – .Net karlshifflett.wordpress.com/2009/10/21/visual-studio-2010-beta2-sample-data-project-templates – view page – cached Displaying Sample Data in the WPF and Silverlight Designer for Visual Studio 2010 Beta2 is now very easy with the DesignData markup extension. — From the page [...]

  2. [...] Visual Studio 2010 Beta2 Sample Data Project Templates (Karl Shifflett) [...]

  3. Social comments and analytics for this post…

    This post was mentioned on Twitter by wpfblogger: received: Visual Studio 2010 Beta2 Sample Data Project Templates http://bit.ly/n0z08

  4. [...] I have explained the d:DesignData MarkupExtension in detail in this blog post:  Visual Studio 2010 Beta2 Sample Data Project Templates. [...]

  5. [...] Updated Code: Visual Studio 2010 Beta2 Sample Data Project Templates I have updated the project templates Visual Studio 2010 Beta2 Sample Data Project Templates.  [...]

  6. [...] to Karl Shifflet and Unni Ravindranathan, I’ve finally gotten my head around a superior method of populating [...]

  7. Karl,

    This is really cool, however. I know this is an “out of band” release and this four templates are better than no templates, but a lot of people will start with the RIA Services, Navigation Template, Class Libraries or migrate existing projects, which won’t support this scenarios. So it’s important to just mention how to add it without the need of the templates.

    There’re two simple changes (for any Silverlight Project). Add the included DesignData.targets file to the PropertiesFolder in the solution and then modify the project file and Import it after

    That goes after the line

    • When the Visual Studio RC comes out, the tempaltes will no longer be needed and will be removed from the Gallery since the required Build Actions will be in Visual Studio.

      I’ll post about this when the RC is public.

      Cheers,

      Karl

  8. [...] You can also set the d:DataContext to d:DesignData. DesignData can use its Source property to locate a Xaml based file that contains the design-time data that you want to use. That way, you can build your design-time VMs in Xaml instead of code and have the tool pick up that as the data context. There is a lot more information about it here. [...]

  9. ga001 says:

    Karl, the link to the video seems to be broken. Is it available elsewhere please?
    Cheers, Gary

Follow

Get every new post delivered to your Inbox.

Join 247 other followers