107

In Winforms you can say

if ( DesignMode )
{
  // Do something that only happens on Design mode
}

is there something like this in WPF?

Russ
  • 12,312
  • 20
  • 59
  • 78

5 Answers5

164

Indeed there is:

System.ComponentModel.DesignerProperties.GetIsInDesignMode

Example:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

public class MyUserControl : UserControl
{
    public MyUserControl()
    {
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            // Design-mode specific functionality
        }
    }
}
Dave
  • 1,338
  • 12
  • 17
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • I applied your solution in my application but it doesn't work. I asked it here http://stackoverflow.com/questions/3987439/init-grid-row-height-doesnt-work. If you would, please join us and discuss. – Nam G VU Oct 21 '10 at 12:31
  • @serhio Thanks for pointing that out. Are you aware of any workaround? Btw it seems that it doesn't work in Silverlight either: http://connect.microsoft.com/VisualStudio/feedback/details/371837/designerproperties-getisindesignmode-doesnt-work-with-silverlight-pages – Enrico Campidoglio Nov 08 '10 at 19:31
  • In VS2019 switch `Enable project code` must be enabled (or Menu->Design-> Run Project Code). – marbel82 Feb 18 '20 at 08:05
48

In some cases I need to know, whether a call to my non-UI class is initiated by the designer (like if I create a DataContext class from XAML). Then the approach from this MSDN article is helpful:

// Check for design mode. 
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) 
{
    //in Design mode
}
Massimiliano
  • 16,770
  • 10
  • 69
  • 112
  • I applied your solution in my application but it doesn't work. I asked it here http://stackoverflow.com/questions/3987439/init-grid-row-height-doesnt-work. If you would, please join us and discuss. – Nam G VU Oct 21 '10 at 13:22
23

For any WPF Controls hosted in WinForms, DesignerProperties.GetIsInDesignMode(this) does not work.

So, I created a bug in Microsoft Connect and added a workaround:

public static bool IsInDesignMode()
{
    if ( System.Reflection.Assembly.GetExecutingAssembly().Location.Contains( "VisualStudio" ) )
    {
        return true;
    }
    return false;
}
user664939
  • 1,977
  • 2
  • 20
  • 35
serhio
  • 28,010
  • 62
  • 221
  • 374
  • Shouldn't it be `GetEntryAssembly()` instead of `GetExecutingAssembly()`? The latter should be returning the assembly where this property is defined – fjch1997 Jun 17 '17 at 18:40
10

Late answer, I know - but for anyone else who wants to use this in a DataTrigger, or anywhere in XAML in general:

xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework"

<Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=(componentModel:DesignerProperties.IsInDesignMode)}" 
                 Value="True">
        <Setter Property="Visibility" Value="Visible"/>
    </DataTrigger>
</Style.Triggers>
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
-1

Use this one:

if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
    //design only code here
}

(Async and File operations wont work here)

Also, to instantiate a design-time object in XAML (d is the special designer namespace)

<Grid d:DataContext="{d:DesignInstance Type=local:MyViewModel, IsDesignTimeCreatable=True}">
...
</Grid>
Jeson Martajaya
  • 6,996
  • 7
  • 54
  • 56
  • 1
    That class (`Windows.ApplicationModel`) is for Store apps, included in the Windows Runtime API. This is not an out-of-the-box WPF solution if you're just working on a regular Windows desktop application. – qJake Feb 25 '16 at 15:56