Home > Code > Design Time Check for WPF and Silverlight

Design Time Check for WPF and Silverlight

February 10th, 2009

Do you commonly check whether code is being run in Blend or in the Visual Studio design surface? Often times, this is necessary to load up sample data or to prevent code that could potentially break the “Blendability” of a WPF or Silverlight app.

In WPF, you can call the DesignerProperties.GetIsInDesignMode for it. In Silverlight, you can check for HtmlPage.IsEnabled property.

I check for design mode often and needed a simple, consistent method to mask the DesignerProperties call in WPF. In Silverlight, I wanted to hide the hacky HtmlPage.IsEnabled check and change it from being a not(!) check to a positive check. Also, when Silverlight support the DesignerProperties.GetIsInDesignMode method, I can just easily change my check in one place. With some refinements from friends at work, I condensed it to a single static property in App.xaml.cs that I can use throughout my application.

In App.xaml.cs:

public static bool IsInDesignMode
{
    get
    {
        #if Silverlight
            return !HtmlPage.IsEnabled;;
        #else
            return DesignerProperties.GetIsInDesignMode(new DependencyObject());
        #endif
    }
}

Note that the precompiler directives can be removed if you’re just targeting WPF or just targeting Silverlight

Here’s how I use it:

void MainView_Loaded(object sender, RoutedEventArgs e)
{
    if (App.IsInDesignMode)
    {
        LoadSampleData();
    }
    else
    {
        LoadRunTimeData();
    }
}

alan.le Code , ,

  1. February 10th, 2009 at 07:28 | #1

    What about making that an extension method of some sort, did you try that?

  2. Owen Pellegrin
    February 11th, 2009 at 10:20 | #2

    You can also provide design-time support that might work for the case of loading sample data; check out DesignModeValueProvider http://is.gd/jbXb

    It’s not a solution for behaviors that might break the designer, but it would let you substitute some dummy data for the data that’s configured at design-time.

  3. alan.le
    February 11th, 2009 at 10:25 | #3

    Keith, interesting point. You’re right, I can probably do this as an extension method for the WPF window class.

    Owen, I haven’t encountered the DesignModeValueProvider before. definitely going to read up on it now.

  4. February 17th, 2009 at 08:47 | #4

    The problem with using HtmlPage.IsEnabled in Silverlight is that many times if you’re using a hosted xap or have turned off html access then you’ll end up in design mode even when you’re not. Seems like there must be a better way, but I still haven’t found it.

  5. alan.le
    February 17th, 2009 at 10:14 | #5

    Bryan, I agree. But currently that’s the only way that I’ve found to work. By encapsulating it in one area, I’m hoping to minimize the changes that I need to make when there is a better way to detect design mode.

  6. Steve Drake
    October 26th, 2010 at 08:02 | #6

    Application.Current will be null if in Visual Studio

  7. Taz
    December 10th, 2010 at 08:47 | #7

    @Steve Drake

    That works very nicely

  8. Jnn
    April 2nd, 2011 at 08:19 | #8

    Simple and easy, tanks

  1. February 25th, 2009 at 17:02 | #1
  2. February 27th, 2009 at 01:55 | #2
  3. October 3rd, 2009 at 06:59 | #3