d:DesignInstance in Depth

Update: I have contacted WPF “User education” team at Microsoft about the lack of documentation. The response was: “I’ve looked into this and there was some confusion around whether or not these were publicly exposed. Thanks for letting us know about this error. We are now working on getting these documented.”

There is currently no formal documentation for the DesignInstanceExtension (which you will see in XAML as d:DesignInstance).

The best two articles are by Unni and Karl but there are a few things they don’t cover.

This is what I have worked out by trial and error:

According to Intellisense DesignInstance has got 3 parameters: Type, IsDesignTimeCreatable and CreateList.

Type This is the type of the object for which the design-time data binding “shape” will be derived
IsDesignTimeCreatable (Defaults to false)

If this is true then a new instance of your type will be created at designtime. If you want data to actually appear in the designer you will need to make sure suitable properties are being set in your constructor. The best way to do this is to create a derived type of your model type, where you are free to set properties to default values without polluting your real types. If your model type implements an interface even better – you can create a design time model that way.

If this is false VS/Blend will use reflection to create a dummy type with the same shape as your specified Type. The problem with this is the dummy type will have no instance data in it thus you will not see anything in the designer. When this is false you are really only getting the benefit of assisted type-aware data binding. (If you need lots of sample data you are better off using the DesignData extension)

CreateList (Defaults to false)

If true the DesignInstance will actually be created as a list of the specified Type. From what I can tell this is the only way to specify your design instance as a collection (I briefly tried to pass a generic list as the Type parameter but could not get it to work, hence why I expect this parameter exists).

Note that DesignInstance and DesignData are mutually exclusive mark-up extensions. You cannot set both DesignInstance and DesignData extensions when setting your DataContext.

In general the main use of DesignInstance extension is to allow you to use the data binding tools within Blend and Visual Studio. It can give you some sample data if you set properties in your constructor.

To be able to design effectively you really need plentiful and varied sample data. For the best design experience you should consider using the DesignData extension – the drawbacks being the slightly increased upfront cost of creating a XAML file containing your sample data, and the increased complexity of refactoring the XAML whenever your classes change.

5 thoughts on “d:DesignInstance in Depth

  1. Pingback: Exploring Design-Time Data in Blend 4 - Christopher Bennage - Devlicio.us - Just the Tasty Bits

  2. John Leidegren

    The thing I didn’t realize was that IsDesignTimeCreatable defaults to false. That has me pretty much going nuts over this. Why would you not specify this!? Well. Thanks for sharing this gem.

Leave a Reply

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