WPF Controls - how to avoid designer nastiness
Posted by: Mark Shurmer
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:
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



You must be logged-in to post a comment. Log-in/Register