In Winforms you can say
if ( DesignMode )
{
// Do something that only happens on Design mode
}
is there something like this in WPF?
In Winforms you can say
if ( DesignMode )
{
// Do something that only happens on Design mode
}
is there something like this in WPF?
Indeed there is:
System.ComponentModel.DesignerProperties.GetIsInDesignMode
Example:
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
}
If you want to see an example of using the DesignMode to populate controls during design time check out my blog post 'how to use the DesignerProperties.GetIsInDesignMode method to populate controls with design time data in WPF'