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