tags:

views:

1556

answers:

3

In Winforms you can say

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

is there something like this in WPF?

+15  A: 

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
        }
    }
}
Enrico Campidoglio
I applied your solution in my application but it doesn't work. I asked it here http://stackoverflow.com/questions/3987439/init-grid-row-height-doesnt-work. If you would, please join us and discuss.
Nam Gi VU
+4  A: 

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
}
Yacoder
I applied your solution in my application but it doesn't work. I asked it here http://stackoverflow.com/questions/3987439/init-grid-row-height-doesnt-work. If you would, please join us and discuss.
Nam Gi VU
A: 

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'

Jag Reehal