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();
    }
}

Code , ,

  1. May 17th, 2014 at 07:08 | #1

    First off I want to say great blog! I had a quick question that I’d like
    to ask if you don’t mind. I was curious to know how you center yourself and clear your mind before writing.
    I have had a tough time clearing my thoughts in getting my ideas out there.
    I truly do enjoy writing however it just seems
    like the first 10 to 15 minutes tend to be wasted just trying to figure out how to begin. Any recommendations or tips?
    Thanks!

  2. May 20th, 2014 at 03:04 | #2

    Most Web design and development companies possess skills in one or
    the other. By clicking your choice of background, the blue screen will be replaced by your new background.
    Click continue at the right lower end of the page, and
    do so to the end.

  3. May 23rd, 2014 at 23:38 | #3

    They create a mobile version of a website for your business in
    a more task-based manner because majority of users are looking for something specific and urgent.
    On the other hand, there are land properties out there which already have developers and builders to do
    the construction for you. You could be tying up your money and limiting your potential investment – if you were to purchase an investment property for $100,000
    with cash you would have a single property.

  4. May 26th, 2014 at 05:52 | #4

    Just desire to say your article is as astonishing. The clearness in your post is just spectacular and i can assume you
    are an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post.
    Thanks a million and please keep up the rewarding work.

  5. May 26th, 2014 at 21:57 | #5

    Ahead of I got myself that, nevertheless, I did search Wikipedia and read
    up with what the foreign currency market was about.

  6. June 3rd, 2014 at 20:44 | #6

    Give it a Shot: If you happen to know someone in the Hollywood, try to get a role
    as a movie extra.

  7. June 4th, 2014 at 22:36 | #7

    Jelqing is known to be an ancient male enhancement technique.

    To a lesser extent, the girth also increases.

  8. June 8th, 2014 at 19:25 | #8

    It is the best time to make a few plans for the longer term and it is
    time to be happy. I’ve read this post and if I may just I want to counsel you some interesting things or advice.
    Maybe you can write next articles referring to this article.

    I wish to read more things about it!

  9. June 9th, 2014 at 16:27 | #9

    I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to design my own blog and would like to know where u
    got this from. appreciate it

  10. June 13th, 2014 at 08:09 | #10

    Yours is a primary example of enlightening writing.
    I think my students could learn lots from your way of writing and your articles.
    I may share this article with them.

  11. June 13th, 2014 at 17:48 | #11

    Hi, i think that i noticed you visited my blog
    thus i got here to return the choose?.I’m trying to to find things to enhance my website!I guess its ok to use some of your concepts!!

  12. June 16th, 2014 at 08:53 | #12

    Posting and reading ads are free, and there are dozens of
    ads in the transportation category added every day.
    Craigslist Classified Website Flickr Image Hosting Imag – Shack Image Hosting.
    I hope the tailor has adjusted the sleeves adequately so I can wear it this weekend.

  13. June 18th, 2014 at 01:01 | #13

    Pretty section of content. I just stumbled upon your website
    and in accession capital to assert that I get actually
    enjoyed account your blog posts. Any way I will be subscribing to your augment and even I achievement you
    access consistently rapidly.

  14. Johne236
    June 20th, 2014 at 07:06 | #14

    Hi Dear, are you in fact visiting this website daily, if so then you will definitely take fastidious knowledge. cdgekgdgedfb

  15. June 21st, 2014 at 03:21 | #15

    So what is the Paleo Diet plan?

  16. June 21st, 2014 at 23:12 | #16

    Incredible! This blog looks just like my old one!
    It’s on a entirely different subject but it has pretty
    much the same page layout and design. Superb choice of colors!

  17. June 22nd, 2014 at 20:02 | #17

    At this moment I am ready to do my breakfast, afterward having my breakfast coming over again to read other news.

  18. June 23rd, 2014 at 21:55 | #18

    Going through it, the user is provided with answers
    to his queries. Nearly all web pages have hyperlinks.

  19. July 11th, 2014 at 07:50 | #19

    It does not matter what your skill level on carpentry is, you will have the ability to
    easily adhere to the step by step plans in the entire project.

  20. July 11th, 2014 at 19:37 | #20

    You should be careful, however, to make certain it is not overpowering your ecommerce product pictures.
    If you see that these answers are exceptional,
    then it must be the right choice. A full-featured shopping cart software, product catalog, and
    management system with extensive customization capabilities are included in these ecommerce solutions.

  21. July 12th, 2014 at 19:07 | #21

    Hey! This post couldn’t be written any better!
    Reading through this post reminds me of my previous room mate!
    He always kept talking about this. I will forward
    this post to him. Pretty sure he will have a good
    read. Many thanks for sharing!

  22. July 23rd, 2014 at 09:53 | #22

    We are a group of volunteers and opening a new scheme in our
    community. Your web site offered us with valuable info to work on. You have done a formidable job and
    our entire community will be grateful to you.

  23. July 28th, 2014 at 00:15 | #23

    Useful information. Fortunate me I discovered your site unintentionally,
    and I’m stunned why this twist of fate did not took place in advance!
    I bookmarked it.

  24. August 13th, 2014 at 23:55 | #24

    Can you tell me how you draw new visitors to your web site?
    How you obtain traffic, how you brand your website and how
    you ensure visitors aare querstions I truly would like to know the answers to

Comment pages
  1. February 25th, 2009 at 17:02 | #1
  2. February 27th, 2009 at 01:55 | #2
  3. October 3rd, 2009 at 06:59 | #3
  4. May 21st, 2014 at 14:55 | #4
  5. August 20th, 2014 at 22:29 | #5