Thursday, November 5, 2009

How to check if WPF control is in design mode

Probably you all know it, still sharing it with the community. The trick is pretty simple and worth knowing. Here is the summery.

Problem: The wpf designer can not render the control

Reason: In constructor of the control some data service is been called which is available only in runtime.

A method from System.ComponentModel.DesignerProperties would save our soul this time, the method name is “GetIsInDesignMode”. Using this method you can check if the control right now is been in design mode or not, and don’t call any data service if its in design mode, Isn’t it pretty simple. Bellow a sample uses is given.

if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
     ///your code goes here
}

References

http://msdn.microsoft.com/en-us/library/system.componentmodel.designerproperties.getisindesignmode.aspx