Is there a DesignMode property in WPF?
Asked Answered
A

5

108

In Winforms you can say

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

is there something like this in WPF?

Alica answered 8/1, 2009 at 20:26 Comment(1)
Note that GetIsInDesignMode suffers from the same enormous bug as the DesignMode property – Cachet
F
163

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
        }
    }
}
Feint answered 8/1, 2009 at 21:35 Comment(3)
I applied your solution in my application but it doesn't work. I asked it here #3987939. If you would, please join us and discuss. – Ratite
@serhio Thanks for pointing that out. Are you aware of any workaround? Btw it seems that it doesn't work in Silverlight either: connect.microsoft.com/VisualStudio/feedback/details/371837/… – Feint
In VS2019 switch Enable project code must be enabled (or Menu->Design->πŸ—Ή Run Project Code). – Maturate
B
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
}
Bifocals answered 4/1, 2010 at 17:6 Comment(1)
I applied your solution in my application but it doesn't work. I asked it here #3987939. If you would, please join us and discuss. – Ratite
C
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;
}
Coagulum answered 8/11, 2010 at 19:50 Comment(1)
Shouldn't it be GetEntryAssembly() instead of GetExecutingAssembly()? The latter should be returning the assembly where this property is defined – Zollie
A
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>
Asomatous answered 30/9, 2016 at 14:34 Comment(0)
S
-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>
Synchronism answered 7/4, 2015 at 19:4 Comment(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. – Facies

© 2022 - 2024 β€” McMap. All rights reserved.