WPF Reflections

May 22 2008   11:33AM GMT

WPF Controls – how to avoid designer nastiness

Mark Shurmer Profile: MarkWPF

Have you ever written any user controls or custom controls?
The difference between them is that with a custom control you have full scope to render it (plus you have to do it), whereas a user control is a container to amalgamate other controls.

With both of them though, you need to make sure that any calls made out of them are minimised.

Why?

Well because when they are created in WPF designers like Visual Studio 2008 or Expression Blend, they will be instantiated and therefore constructors will be called.
If you have calls out to databases etc, you will get a nasty looking exception thrown, or just the Visual Studio 2008 problem loading display:

blog-22may2008.JPG

How do you sort this problem?

By using the GetIsInDesignMode static method of DesignerProperties, like so
if (!DesignerProperties.GetIsInDesignMode(App.Current.MainWindow))
// do something you don’t want to appear in designer

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: