Exploring Design-Time Data in Blend 4

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}" />

 


Posted 03-26-2010 4:26 PM by Christopher Bennage

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)