Detecting design time mode in WPF and Silverlight

We already talked often about providing design time data to your designers in Windows Presentation Foundation and in Silverlight, thus enabling them to work visually in design editors such as Expression Blend or the Visual Studio designer (codenamed Cider).

Read the rest of this entry »

Print | posted on Saturday, September 5, 2009 12:07 AM

Feedback

# re: Detecting design time mode in WPF and Silverlight

left by Daniel Vaughan at 9/5/2009 1:42 AM Gravatar
Very useful. Thanks Laurent.

# Possibly solution?

left by Grumpydev at 9/5/2009 5:49 AM Gravatar
This is a possible solution, if a little messy. I've tested it with a WPF app and a WPF control in a WinForms app. I've also added an additional conditional that always returns "false" if we aren't in debug mode so our release software doesn't have to bother checking:

private static bool? _isInDesignMode;

/// <summary>
/// Gets a value indicating whether the control is in design mode (running in Blend
/// or Visual Studio).
/// </summary>
public static bool IsInDesignModeStatic
{
get
{
if (!_isInDesignMode.HasValue)
{
#if DEBUG
#if SILVERLIGHT
_isInDesignMode = DesignerProperties.IsInDesignTool;
#else
var prop = DesignerProperties.IsInDesignModeProperty;
_isInDesignMode
= (bool)DependencyPropertyDescriptor
.FromProperty(prop, typeof(FrameworkElement))
.Metadata.DefaultValue;

if (!_isInDesignMode.Value)
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.StartsWith(@"devenv"))
_isInDesignMode = true;
#endif

#else
_isInDesignMode = false;
#endif
}
return _isInDesignMode.Value;
}
}

# re: Detecting design time mode in WPF and Silverlight

left by Laurent at 9/5/2009 6:47 AM Gravatar
@Daniel, thanks!

@Steve: You know, it's funny, but the other day I was chatting with Josh Smith and he told me "why don't you just check the process name" LOL

You know what, what the heck, I will just use your solution. If you and Josh Smith both think it is viable, it is more than good enough for me ha ha ha.

Nice touch about the #if DEBUG too. It makes sense, since Blend always runs the DEBUG config anyway.

Cheers,
Laurent

# re: Detecting design time mode in WPF and Silverlight

left by Grumpydev at 9/5/2009 7:01 AM Gravatar
My only concern with the debug conditional would be for people shipping binary controls. If they only ship release builds then design mode won't work. No idea if anyone would actually do that though :-)

# re: Detecting design time mode in WPF and Silverlight

left by Laurent at 10/4/2009 12:16 AM Gravatar
Update: In V2 of the MVVM Light Toolkit, I removed the #if DEBUG which was indeed causing issues in release mode. It was a bad good idea :) The result is now consistent again in Release and in Debug mode.

# re: Detecting design time mode in WPF and Silverlight

left by Tee at 9/28/2010 12:53 AM Gravatar
Hi Laurent,

I am running Expression Blend 4 (RTM) for Windows phone 7 and VS 2010 Express (RTM), and neither IsInDesignModeStatic nor IsInDesignTool return true when running in expression blend 4.

Is it because I am running the express versions?

# re: Detecting design time mode in WPF and Silverlight

left by Laurent at 9/28/2010 4:33 AM Gravatar
Hi,

No it should work just fine. That's weird. Can you try to check if DesignerProperties.IsInDesignTool returns true?

Thanks,
Laurent

# re: Detecting design time mode in WPF and Silverlight

left by Christopher at 11/26/2010 1:25 PM Gravatar
Thank you! This made my day!

# re: Detecting design time mode in WPF and Silverlight

left by Michael Tilelli at 1/10/2011 7:04 PM Gravatar
I found this code to be useful in code behind:

public override void EndInit()
{
if (!DesignerProperties.GetIsInDesignMode(this))
{
// insert code here
}
base.EndInit();
}

Then you don't need to write an extra method

# re: Detecting design time mode in WPF and Silverlight

left by Laurent at 1/11/2011 8:50 AM Gravatar
Hi,

It does not work in WPF. This is why I abstracted this in the MVVM Light toolkit.

Cheers,
Laurent

# re: Detecting design time mode in WPF and Silverlight

left by Jordan at 3/30/2011 8:46 PM Gravatar
Great Stuff Laurent, Thank You!

# re: Detecting design time mode in WPF and Silverlight

left by Christian Scott at 5/20/2011 7:55 AM Gravatar
It seems Blend 4 actually runs the code-behind without problems so you don't need to differentiate between design/runtime. Can anyone confirm?

Regards,
Christian

# re: Detecting design time mode in WPF and Silverlight

left by Laurent Bugnion at 5/20/2011 8:36 AM Gravatar
Hi,

Three things:

• Strictly speaking Blend does not run code behind, but only code that is hooked in the resources in XAML. This is how MVVM Light applications are "wired", for this exact reason.
• Some code runs just fine, but other code fails in Blend. Typically calls to web services or attempts to get data from database will not run in Blend and you will even in some cases get exceptions which prevent to do any visual work.
• Even when the code runs, it can be impossible to get actual data in Blend, for instance if there is a login needed.

In all those cases, design time detection is your friend.

Cheers
Laurent

# re: Detecting design time mode in WPF and Silverlight

left by Christian Scott at 5/20/2011 12:10 PM Gravatar
Thanks. I tried coercing Blend into throwing exceptions by doing some async file reading and stuff. Didn't try the cases you mention.

I would like to request some videos where you do everything MVVM Light Toolkit does, but without MVVM Light Toolkit, and then code the Toolkit from bottom up to show the true benefits. If you're a newbie to WPF/SL/MVVM, the toolkit smooths over stuff you might never fully understand which could be a disadvantage.

Have a great day!

Christian

# re: Detecting design time mode in WPF and Silverlight

left by Luke Puplett at 10/13/2011 1:36 PM Gravatar
Sorry if I'm talking about old stuff but checking the process name might cause a problem for partial trust apps.

MSDN:

[The Process] class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands and Inheritance Demands

# re: Detecting design time mode in WPF and Silverlight

left by DC at 8/23/2012 11:16 PM Gravatar
Awesome! Saved me some design time headaches with a user control.

# re: Detecting design time mode in WPF and Silverlight

left by Alexclub at 2/18/2013 2:36 PM Gravatar
can nothing but agree with all above, though it was hard

# re: Detecting design time mode in WPF and Silverlight

left by Steve at 5/15/2013 7:51 PM Gravatar
bool designTime = System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject());
works for me in WPF.
Comments have been closed on this topic.