DesignData support for Silverlight in Visual Studio 2010 and Blend 4

In order to take advantage of the design-time binding support in Blend (and now VS .NET 2010), especially when using MVVM, you had these options:

1. Set the DataContext in XAML to a StaticResource (like a ViewModel) created for design only, and change it to the real thing at runtime.

Good: use the existing class structure, XAML intellisense and autocompletion.

Bad: the design-time object will get created at runtime too, cannot access private set or readonly properties, must have a parameterless constructor.

2. Create sample data in Blend (XML).

Good: can be created by designers, can use private set or readonly properties, no runtime penalty.

Bad: must be kept in sync with the actual ViewModel, no compile-time checking, no intellisense support.

DesignData

The DesignData combines the advantages of the two options – you can create a XAML with your design values, based on the existing class model.

You can take advantage of intellisense and autocompletion and you’re able to set readonly properties, instantiate classes with no parameterless contructors.

The objects will be created only at design time, so there is no runtime penalty. You also get compile-time checking – you get an error if you try to set an invalid value to a int property, for example.

This functionality has been available since Blend 3, but it was (and still is) kind of hidden – in Blend 3 / VS 2008 you had to manually edit the project file to get it to work.

Fortunately in VS 2010 you don’t need to edit the project file, but there still is no Item Template for DesignData files.

[more]

In order to add a DesignData file, you can add a generic text file to the project and set its extension to “.xaml”.

 

Then change its Build Action to “DesignData”.

 

For this demo we’ll use a ViewModel that has different types of properties (public, private set, readonly, collection).

 

For the Entity class, we’ll have no public parameterless constructor:

We can go ahead and create our design-time data – notice that you have full intellisense support, even for the readonly properties.

If we add a TextBlock to our view, initially we have no databinding support.

 

We need to set the design DataContext for the container to use our DesignData source.

Now we can set the databinding easily:

 

And instantly get the values in the designer:

 

We get full support in Blend, too:

 

Of course the ItemsControl has no ItemTemplate set, so we get this:

 

We can create a template and take advantage of the databinding support in the template editing too:

 

We have a full image of how our design looks like with data, without having to compile and run the application.

In Visual Studio too:

Conclusion

Although still “hidden”, the DesignData functionality is the best option if you want to take advantage of the design-time support in Visual Studio 2010 and Blend, especially if you are using the Model-View-ViewModel pattern.

Update (thanks to Adrian Vinca):

You can create DesignData easier in Blend 4 – select “Create sample data from class…” in the Data pane:

Select the class you want to use:

You’ll get the generated schema:

And random data created depending on the property type:

This entry was posted in Silverlight. Bookmark the permalink.

One Response to DesignData support for Silverlight in Visual Studio 2010 and Blend 4

  1. Gene says:

    Thank you, very well explained. Timely too, since I have been looking for an implementation using d:Design Data. You’ve rounded up a lot of good information here.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>