Tuesday, March 29, 2011

Silverlight–Detecting design mode

When you need to detect if your view or view-model is being instantiated during design time or runtime, the easiest method is to check the IsInDesignTool static property of the DesignerProperties class.

Even better implement it in your ViewModel’s base.

protected bool IsDesignTime()
{   return DesignerProperties.IsInDesignTool;
}
Note: You don’t need to cache the IsInDesignTool property value as calling it doesn’t result in any methods being called.
FYI, the following code didn’t work when the View was opened in Visual Studio (found them via Google).
DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual)
DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue

No comments:

Post a Comment

Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.