Introduction

In order to provide an editing experience as close as possible to the runtime, Expression Blend executes some of the user defined code (it instantiates controls, data sources, sets properties, etc.). But in some cases, you may want to avoid this behavior (if that code doesn't work properly when executed inside Blend or if it is a more time consuming experience).

This post explains how to detect if your code is executed inside Blend and provide an alternate implementation for that case. For example your datasource could connect to a database and extract the data, but, when running inside Blend, it could generate a minimal sample data which can be used for creating some data templates).

Solution

The following sample uses a custom TextBlock which displays a different message when the application is loaded in a designer (in our case Expression Blend):

	public class MyTextBlock : TextBlock
	{
		public MyTextBlock() : base()
		{
			if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
			{
				this.Text = "Design mode detected!";
			}
			else
			{
				this.Text = "No design mode";
			}
		}
	}

For further information you may check out the documentation for the DesignerProperties Class.