Click here to Skip to main content
14,304,866 members

Blendability Part I – Design Time ViewModel

Rate this:
5.00 (4 votes)
Please Sign up or sign in to vote.
5.00 (4 votes)
17 Jan 2011CPOL
Blendability Part I – Design time ViewModel

In case you haven't added the word ‘Blendability' to your XAML Jargon yet, I’m sure this post will inspire you in doing so.

The Blend-ability term describes how a piece of data model or view model is viewable or designable at design time, whether by Expression Blend or Visual Studio Designer.

Building Silverlight or WPF applications, everybody loves using the MVVM pattern. This pattern greatly decouples the view from its logic and domain model, hence enabling relatively easy unit testing and provides great flexibility. In most cases, there should be no code behind XAML except for the InitializeComponents in the view’s ctor (in case of UserControl). This very important aspect of the MVVM pattern should help the XAML designer in changing the view using any favorite tool, such as Blend, without being bothered about the view’s logic.

Let’s have a look at a little application which uses the MVVM pattern for displaying a simple view.

public class Camera2dInspectionViewModel : NotificationObject
    public ObservableCollection<DefectViewModel> Defects { get; private set; }
    public double Fps { get; set; }
    public double Brightness { get; set; }
    public ImageSource CurrentFrame { get; set; }

The code snippet above is the ViewModel. It represents the view of a 2D camera inspection unit. For simplicity, it exposes only relevant properties to the relevant view.

Some of the properties are straightforward, such as Fps, and others represent collection: Defects.

Let’s take a look at the view itself.

<UserControl d:DesignHeight="300" d:DesignWidth="300">
        <Image Source="{Binding CurrentFrame}" />
        <ListBox ItemsSource="{Binding Defects}">
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="RenderTransform">
                            <TranslateTransform X="{Binding Zone.X}"

                                                Y="{Binding Zone.Y}" />
                            <Run Text="Defect #" />
                            <Run Text="{Binding Id}" />
                        <Rectangle Width="{Binding Zone.Width}"

                                Height="{Binding Zone.Height}" />
        <TextBlock Text="Brightness" />
        <Slider Value="{Binding Brightness}" />
        <TextBlock Text="{Binding Fps}" />

For clarity, I’ve removed brushes and layout properties.

Now, there should be a piece of code gluing the View with the ViewModel by setting the View.DataContext with the ViewModel instance. There are several techniques for doing that: Hardcoded, MEF, Service Locator, Others.

No matter what your favorite technique is, both Expression Blend and Visual Studio are keen to locate the data-context object inside the XAML file at design time. Failure to do so results in null data-context and no data being presented at design time.


How to Instantiate the view-model so it will be Rendered by the View at Design Time?

Well, we can create an instance of the view-model directly within the view’s XAML, but I’m sure that you agree that this is not a best practice and it is not possible in several circumstances such when the view-model has no default constructor and/or have to be instantiated using a DI container or should be created and populated by the business layer itself.

There are several ways of solving this problem. In this post, I’ll cover my favorite one using Design Time only DataContext.

Working with Blend 3, 4 and VS2010, there is a magical designer property called d:DataContext. This property is DataContext for design time only. Using this property, you can keep setting the view’s regular DataContext property with a view-model as usual and then set the view’s magical d:DataContext designer property with a different design-time view-model instance created in XAML. Opening the view at design-time using Blend or Visual Studio, the d:DataContext will take place. Running the application, the regular DataContext will be used.

This solution is almost perfect. Now all we have to do is to generate a design-time view-model from within the XAML file.

Now let’s face it, no one, including the XAML designer likes generating fake data with bare hands, and here comes Blend to the rescue. There is a cool feature in Blend for generating data within XAML. There are several options of course, but one of them best fits our need. We’ll generate data based on our view-model class.

To do so, open the project with Blend, open the relevant view, go to data tab on the right, select project, click on the first small icon on the right and choose “Create Sample Data from class…”. Pick the relevant view-model and then click ok.


At this moment, Blend created a new folder called SampleData and a new XAML file.


If you open the XAML file created, you’ll find the following generated markup:





		<Blendability_Modules_ViewModels:DefectViewModel Id="63">
				<Rect Height="315.65" Width="181.51"

					X="468.12" Y="991.84">
						<Point X="988.41" Y="166.96"/>
						<Size Height="422.96"


Amazingly Blend generated design-time instance from our view-model and populated it with auto-generated random data, no matter if the view-model has no default constructor, or in our case the Fps property is read-only. Also it dug inside the view-model, generated all public properties including collections. I had to cut some XAML for clarity.

Of course, I had to change the data and add missing properties such as CurrentFrame, as nothing is perfect in this world especially software.

Here is the final result of the data I changed:





        <BitmapImage UriSource="/SampleData/Penguins.jpg" />
        <vm:DefectViewModel Id="1">
                <Rect Height="40" Width="40" X="68" Y="15" />
        <vm:DefectViewModel Id="2">
                <Rect Height="25" Width="25" X="180" Y="50" />
        <vm:DefectViewModel Id="3">
                <Rect Height="50" Width="50" X="50" Y="90" />

Now that we have design-time only view-model, let's connect it with the view’s d:DataContext by simply opening the view and drag the sample view-model from the Blend’s data tab into the view’s root element located in the Objects and Timeline tab on the left.


Hurrah… now you can be happy, watching the view rendering the sample view-model at design-time and you can now edit the style of your view’s controls, edit the data template of the Defects collection and see the big picture of your view without running the whole application again and again.

Please feel free to download the demo code from here.

In the next post, I’ll reveal my little research on Blendability of Prism Modules. This one is really a neat feature, so stay tuned.

Image 10Image 11


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tomer Shamam
Architect CodeValue
Israel Israel
Tomer Shamam is a Software Architect and a UI Expert at CodeValue, the home of software experts, based in Israel ( Tomer is a speaker in Microsoft conferences and user groups, and in-house courses. Tomer has years of experience in software development, he holds a B.A degree in computer science, and his writings appear regularly in the Israeli MSDN Pulse, and other popular developer web sites such as CodePlex and The Code Project. About his thoughts and ideas you can read in his blog (

Comments and Discussions

-- There are no messages in this forum --
Posted 17 Jan 2011

Tagged as


6 bookmarked