Click here to Skip to main content
15,891,951 members
Articles / Expression blend

Expression Blend Cannot Open Your Custom Control in Designer?

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
31 Oct 2011CPOL1 min read 5.2K   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:
    C#
    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    {
        throw new InvalidOperationException("Constructor is only meant to be used by designer");
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) MixModes Inc. | Research In Motion
Canada Canada
Ashish worked for Microsoft for a number of years in Microsoft Visual Studio (Architect edition) and Windows Live division as a developer. Before that he was a developer consultant mainly involved in distributed service development / architecture. His main interests are distributed software architecture, patterns and practices and mobile device development.

Currently Ashish serves as a Technical Lead at RIM leading next generation BlackBerry media experience and also runs his own company MixModes Inc. specializing in .NET / WPF / Silverlight technologies. You can visit MixModes at http://mixmodes.com or follow it on Twitter @MixModes

In his free time he is an avid painter, hockey player and enjoys travelling. His blog is at: http://ashishkaila.serveblog.net

Comments and Discussions

 
-- There are no messages in this forum --