How to get Design Mode property in WPF

Last day I needed to check if the code was executing in Design Mode but I couldn’t remember how to do it, so I had to Google again for that matter. That said, I decided to blog about it to prevent wasting my time in a near future.

Jim Nakashima has a nice post about the subject, you can read it here, so I wont get into much detail.

The code to get the design mode property in your control looks like this:

using System.ComponentModel;

private bool IsInDesignMode
{
    get
    {
        return DesignerProperties.GetIsInDesignMode(this);
    }
}

If you don’t have a DependencyObject you can still get the property using the following:

using System.ComponentModel;

private bool IsInDesignMode
{
    get
    {
        return DesignerProperties.GetIsInDesignMode(new DependencyObject());
    }
}

or

using System.ComponentModel;

private bool IsInDesignMode
{
    get
    {
        return (bool)DesignerProperties.IsInDesignModeProperty
            .GetMetadata(typeof(DependencyObject)).DefaultValue;
    }
}

Shout it

4 thoughts on “How to get Design Mode property in WPF

  1. Pingback: Getting a consistent experience with design-time data in WPF (Part I) | Software and UX

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>