How to get Design Mode in WPF

This article explains how you can get DesignMode. Especially, it’ll help don’t break VS Design View and Blend Design View. Also you’ll learn how to set and track IsInDesignMode property in your code.

In many different situation you need to get some data in constructor or OnLoad event, such as: some settings, data from database (I know it’s bad way load big amount of data in main thread) ect.

In these cases VS Design View and Blend Design View is broke. Last day I needed to do this in my project so it was necessary check if the code was executing in Design Mode.

So instead of looking up a DependencyProperty through the AppDomain data context by name, you can check the DependencyProperty defined in the DesignerProperties class.

You can get this value in your code like this:

if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
Content = “In Design Mode”;
}
else
{
Content = “Runtime”;
}

So if you have some of these case in your code I suggest use following in code before of all:

if (DesignerProperties.GetIsInDesignMode(this))

return;

It doesn’t crash Design View in Visual Studio and Blend.

BTW: you can set DesignMode:

System.ComponentModel.DesignerProperties.SetIsInDesignMode(element, newValue);

And Track Changes to the IsInDesignMode Property through the overriding OnPropertyChanged event.


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s