Is there a way to check if WPF is currently executing in design mode or not?
Solution 1
I believe you are looking for GetIsInDesignMode, which takes a DependencyObject.
Ie.
// 'this' is your UI element
DesignerProperties.GetIsInDesignMode(this);
Edit: When using Silverlight / WP7, you should use IsInDesignTool
since GetIsInDesignMode
can sometimes return false while in Visual Studio:
DesignerProperties.IsInDesignTool
Edit: And finally, in the interest of completeness, the equivalent in WinRT / Metro / Windows Store applications is DesignModeEnabled
:
Windows.ApplicationModel.DesignMode.DesignModeEnabled
Solution 2
You can do something like this:
DesignerProperties.GetIsInDesignMode(new DependencyObject());
Solution 3
public static bool InDesignMode()
{
return !(Application.Current is App);
}
Works from anywhere. I use it to stop databound videos from playing in the designer.
Solution 4
There are other (maybe newer) ways of specifying design-time data in WPF, as mentioned in this related answer.
Essentially, you can specify design-time data using a design-time instance of your ViewModel:
d:DataContext="{d:DesignInstance Type=v:MySampleData, IsDesignTimeCreatable=True}"
or by specifying sample data in a XAML file:
d:DataContext="{d:DesignData Source=../DesignData/SamplePage.xaml}">
You have to set the SamplePage.xaml
file properties to:
BuildAction: DesignData
Copy to Output Directory: Do not copy
Custom Tool: [DELETE ANYTHING HERE SO THE FIELD IS EMPTY]
I place these in my UserControl
tag, like this:
<UserControl
...
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
...
d:DesignWidth="640" d:DesignHeight="480"
d:DataContext="...">
At run-time, all of the "d:" design-time tags disappear, so you'll only get your run-time data context, however you choose to set it.
Edit You may also need these lines (I'm not certain, but they seem relevant):
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Solution 5
When Visual Studio auto generated some code for me it used
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
...
}
Related videos on Youtube
Angry Dan
web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info
Updated on January 14, 2022Comments
-
Angry Dan over 2 years
Does anyone know of some global state variable that is available so that I can check if the code is currently executing in design mode (e.g. in Blend or Visual Studio) or not?
It would look something like this:
//pseudo code: if (Application.Current.ExecutingStatus == ExecutingStatus.DesignMode) { ... }
The reason I need this is: when my application is being shown in design mode in Expression Blend, I want the ViewModel to instead use a "Design Customer class" which has mock data in it that the designer can view in design mode.
However, when the application is actually executing, I of course want the ViewModel to use the real Customer class which returns real data.
Currently I solve this by having the designer, before he works on it, go into the ViewModel and change "ApplicationDevelopmentMode.Executing" to "ApplicationDevelopmentMode.Designing":
public CustomersViewModel() { _currentApplicationDevelopmentMode = ApplicationDevelopmentMode.Designing; } public ObservableCollection<Customer> GetAll { get { try { if (_currentApplicationDevelopmentMode == ApplicationDevelopmentMode.Developing) { return Customer.GetAll; } else { return CustomerDesign.GetAll; } } catch (Exception ex) { throw new Exception(ex.Message); } } }
-
aL3891 about 13 yearsAs a side note, IsInDesignMode is actually an attached property, so you can use it in a binding from xaml as well. Might not be the most common use though :)
-
Pat about 13 yearsThis method also works for making ViewModels designer-friendly (since they are not DependencyObjects themselves).
-
Rico Suter almost 13 yearsDependencyObject has a protected constructor - define
internal class MyDependencyObject : DependencyObject {}
and usenew MyDependencyObject
instead ofDependencyObject
-
Sevenate over 10 yearsThanks for keeping the answer up to date with latest XAML "applications" like WinRT and WP.
-
John Leidegren almost 9 yearsA variation on the above
Application.Current.MainWindow == null
though I like the type test better, more direct. It also appears as if the designer hosted in Visual Studio adds resources, so here's another way to do it (if you don't have access to the specificApp
type in the library hosting your code)((bool)Application.Current.Resources["ExpressionUseLayoutRounding"])
. Need checks if the resource is not there though but it does work in designer context. -
Peter Duniho over 8 years@RicoSuter:
DependencyObject
's constructor ispublic
. -
aruno over 7 yearsif doing this in a viewmodel you probably want to abstract it away into a static class and store the result as a static boolean
-
Toby Smith almost 5 yearsThis worked for me where I needed to know if I was running in design time from within a viewModel and couldn't use Windows libraries. I know it's a very small amount of reflection but I didn't like the thought of it running in production so I wrapped this code in an
#if DEBUG
else return false. Is there any reason to not do that? -
marbel82 over 4 yearsIn VS2019 switch
Enable project code
must be enabled (or Menu->Design->🗹 Run Project Code). -
GregorMohorko over 3 yearsThis answer doesn't work anywhere. For instance, in a library you don't have the App class. ;)
-
IFink over 3 yearsExcellent and simple solution.
-
Martin Braun over 2 years@GregorMohorko It's the right answer if you need to check this in the static constructor of
App
. -
GregorMohorko over 2 years@MartinBraun Yes, there it will work. I was replying to his
Works from anywhere.
claim. -
Martin Braun over 2 years@GregorMohorko Sorry yes, mis-read your words as "everywhere". You are right, you need to have access to
App
and I would even consider it bad to accessApp
from anything butApp
itself to ensure modularity. -
Martin Braun over 2 yearsIt seems
Application.Current is App
returns false in Release mode and I don't know why. -
Arturo Torres Sánchez over 2 yearsI tried this technique in VS2022 and .NET 6, but the appname I get is
WpfSurface
instead.