Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Exploring Design-Time Data in Blend 4

DZone's Guide to

Exploring Design-Time Data in Blend 4

·
Free Resource

I’ve gleaned most of what I have learned about this topic from Unni, Karl, and Jack. I recommend checking out their posts for additional information.

d:DataContext

The d:DataContext attribute allows you to establish a data context that is only for design time. You can use it the same way (more or less) that you use the real DataContext. Two options are available for providing data for the design-time data context. They are the markup extensions d:DesignInstance and d:DesignData.

{d:DesignInstance}

This new markup extension has three properties:

Type The type of the instance that we would like to use for design-time data context. This is the default property, so you can omit it and just provide the type value.
IsDesignTimeCreatable True: create an instance of the type and use it for the design-time data.
False: reflect over the type so that the data binding expression builder is aware of the members.
CreateList Do we want a list of instances or a single instance?

This is mostly for giving you the shape of the data that you are binding to. Though you will see the actual property values at design-time, so you can create a new type with the same shape as your real viewmodel, but with data to use for design-time (perhaps by interface, perhaps by subclassing). This approach can be tedious though, especially in cases where you need lists in design-time data.

{d:DesignData}

This markup extension has a single property, Source, that points to a xaml file describing the design-time data. The best way to create this xaml file is using Blend. Blend can automatically generate Lorem Ipsum for your viewmodels (which is kinda cool). I’ll show you how to do that in a follow-up post.

N.B. While everything works brilliantly in Blend, I wasn’t able to get d:DesignData to work for Silverlight in Cider (Visual Studio 2010), it complained about the leading forward slash (/) but still didn’t work when I removed it. However, removing the forward slash broke it for Blend.

A Quick Example

<UserControl x:Class="DesignTimeTest.MyView"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:DesignTimeTest"
d:DesignHeight="300"
d:DesignWidth="400"
d:DataContext="{d:DesignInstance local:MyViewModel, IsDesignTimeCreatable=True}" />

 

Topics:

Published at DZone with permission of Christopher Bennage, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}