You can do something like this:
using System.ComponentModel; using System.Windows; /// <summary> /// WPF Design Mode helper class. /// </summary> public static class DesignMode { private static bool? _isInDesignMode; /// <summary> /// Gets a value indicating whether the control is in design mode (running in Blend /// or Visual Studio). /// </summary> public static bool IsInDesignMode { get { if (!_isInDesignMode.HasValue) { var prop = DesignerProperties.IsInDesignModeProperty; _isInDesignMode = (bool)DependencyPropertyDescriptor .FromProperty(prop, typeof(FrameworkElement)) .Metadata.DefaultValue; } return _isInDesignMode.Value; } } }
Then, as the first line in the constructor of your view (or view model), you can do something like this:
if (DesignMode.IsInDesignMode) { return; }
That way, your code will only run when it runs.
source share