Is there a DesignMode property in 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 } }}
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}
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;}