Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Expression Blend Cannot Open Your Custom Control in Designer?

0.00/5 (No votes)
31 Oct 2011 1  
Expression Blend and Visual Studio designer(s) have stringent conditions that a user control has to follow in order for its type to be even declared compatible for loading.

Sometimes, especially when working with an offline-designer, you may end up in situations where Expression Blend can no longer open your user controls. You may get some error like:

Cannot create an instance of [User Control class]

A lot of times, even attaching a debugger to Blend/VS and putting breakpoints in your constructor / breaking on error would not yield any benefits. Why?

The reason is Expression Blend and Visual Studio designer(s) have stringent conditions that a user control has to follow in order for its type to be even declared compatible for loading. So if any of these conditions are violated, type will be declared unfit and designer won't even bother locating / invoking the constructor of your user control. I have identified two such conditions (there may be more):

  1. In no path of your inheritance hierarchy can there be an abstract class - So if you have a control that is derived from an abstract control class, you are out of luck. Even though your end control is concrete, it won't work!
  2. You need a public default constructor at each level of inheritance hierarchy - That means even if you have a default constructor in your most derived user control class, it is not sufficient. All base classes must also have public default constructors (even if none of the base classes call it)! The good news is that you can assert if these constructors are called in designer only mode by the following code:
    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    {
        throw new InvalidOperationException("Constructor is only meant to be used by designer");
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here