Feed Icon  


  • Bryant Likes
  • Send mail to the author(s) E-mail
  • twitter
  • View Bryant Likes's profile on LinkedIn
  • del.icio.us
Get Microsoft Silverlight
by clicking "Install Microsoft Silverlight" you accept the
Silverlight license agreement

Hosting By

Hot Topics


Open Source Projects



Detecting Design Mode in Silverlight

Posted in Silverlight at Wednesday, 25 February 2009 01:46 Pacific Standard Time

One of the things I’ve been trying to getting a better understanding of is how to make the Silverlight projects I work on more blendable:

In general, WPF and Silverlight controls should be "blendable". ItemsControls need to display representative data within the design surface.

The problem, at least for me, is that every example out there to detect design mode uses:

var designMode = !HtmlPage.IsEnabled;

Since the Html Bridge is disable inside of Blend, this does work for the most part, but what about when your xap is hosted on another server? In this case the Html Bridge is disabled by default so if someone doesn’t configure it correctly they will get your design time data.

Mode HtmlPage.IsEnabled
Blend false
Visual Studio false
Local Xap true
Remote Xap false*
Streaming Silverlight true**

* This can be changed to true, but it is disabled by default.
** Enabled by default

So I was trying to come up with another method to detect design mode in Silverlight and here is the best I have come up with so far:

public bool IsDesignTime()
        var host = Application.Current.Host.Source;
        return false;
        return true;

What happens is that Application.Current.Host.Source works great when the plugin is hosted in a web page and will return the path to the xap file, but in design mode trying to access that property throws an exception. So if you hit the exception then you’re in design mode, otherwise you’re in a web page. Not super elegant but it feels better to me than checking if the Html Bridge is enabled since that isn’t a true check.

Update: As Tom mentions in the comments, you can also use DesignerProperties.GetIsInDesignMode. But if your goal is to make your project more blendable then Visual Studio support might not be important.

Mode DesignerProperties.GetIsInDesignMode
Blend true
Visual Studio false
Local Xap false
Remote Xap false
Streaming Silverlight false

So with this check worst case you get no data in the Visual Studio designer, but the Visual Studio designer isn’t that great anyway. Blend is the real goal. So instead instead of the above code you can use this code instead:

public bool IsDesignTime()
    return DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual);

Thanks for the tip Tom!

Wednesday, 25 February 2009 05:44:29 (Pacific Standard Time, UTC-08:00)
You can use DesignerProperties.GetIsInDesignMode in Silverlight. It works properly with Blend. It's probably still broken in VS, but VS design support for Silverlight is a joke anyway so it hardly seems like a great loss. (Maybe that's why they never bothered to fix it.)
Wednesday, 25 February 2009 14:59:55 (Pacific Standard Time, UTC-08:00)
Hi Bryan,

Nice blog post, thanks for compiling all that information together.
We are working on converging detecting design time to a single easy to use method.

From a Blend perspective you should really just use DesignerProperties as you've mentioned.

For VS (and this is a little unknown trick) the Application.Current is going to be "System.Windows.Application" and not an inheritance from it. Which is never the case for a real-world app. So you could use that to detect while you're running the VS designer.

In the Silverlight Toolkit source code somewhere under DataVisualzation you'll find an extended designer properties class that uses that heuristics to figure out when you're in design mode or not.

-- Justin Angel
Microsoft Silverlight Toolkit Program Manager
Thursday, 26 February 2009 06:55:27 (Pacific Standard Time, UTC-08:00)
Bryan, this post is excellent.

When I started doing the IsDesignMode check, System.Component model was not in SL yet and the only way was to use !HtmlPage.IsEnabled. Now that I know that DesignerProperties is available, I need to update my projects and blog post to use that instead.

Comments are closed.