Welcome to MSDN Blogs Sign in | Join | Help

How to detect if your code is being executed inside Expression Blend

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.

Published Friday, July 27, 2007 7:12 PM by adrianvinca

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker