Is there a DesignMode property in WPF?

Wpf.Net 3.5

Wpf Problem Overview


In Winforms you can say

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

is there something like this in WPF?

Wpf Solutions


Solution 1 - Wpf

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
        }
    }
}

Solution 2 - Wpf

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
}

Solution 3 - Wpf

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

Solution 4 - Wpf

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>

Solution 5 - Wpf

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRussView Question on Stackoverflow
Solution 1 - WpfEnrico CampidoglioView Answer on Stackoverflow
Solution 2 - WpfMax GalkinView Answer on Stackoverflow
Solution 3 - WpfserhioView Answer on Stackoverflow
Solution 4 - WpfManfred RadlwimmerView Answer on Stackoverflow
Solution 5 - WpfJeson MartajayaView Answer on Stackoverflow