RecentComments

Comment RSS

WPF Designer Error "Could not create an instance of type"

by Ioannis 20. September 2009 09:48

Suppose that in a WPF application there are some custom User Controls implemented.

During development in some windows that use the custom User Control you may find that the designer cannot load them and instead it gives the following error:

"Could not create an instance of type YOURCONTROLHERE"

This happens because the designer runs the constructor for the User Control whenever it needs to display it in design mode. Within the constructor something throws an exception. Sometimes this does not mean necessarily that the excepion will be thrown during runtime. So you need a way to allow the designer show the window but also not alter the code that runs correctly at runtime.

A way to solve this is to change your code as follows:

Original:

public UserControlConstructor()
{
    InitializeComponent();
    //Code that throws the exception
}

Change To:

public UserControlConstructor()
{
    InitializeComponent();
    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    {
        //Code that throws the exception
    }
}

The directive GetIsInDesignMode(this) does the trick of knowing whether the constructor is run in Design or Runtime mode.

 

 kick it on DotNetKicks.com Shout it

Tags:

WPF

Comments

9/21/2009 9:47:31 AM #

trackback

WPF Designer Error

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

9/21/2009 9:51:26 AM #

trackback

C# and .NET Tips and Tricks | WPF Designer Error "Could not create an instance of type"

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

Add comment


(Will show your Gravatar icon)

Enter the word
CAPTCHA word
Add 1 to the number above


  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7

Programming Blogs - BlogCatalog Blog Directory Add to Technorati Favorites

MVP Award

Ioannis Panagopoulos





This blog is using BlogEngine.Net and is hosted in the hoster below. I have not experienced any problems installing BlogEngine.Net in the host and I am satisfied with the host's response times. Therefore I recommend it.


DiscountASP Add