views:

458

answers:

4

I have been having real problems getting the WPF designer to work in VS 2008 when i use the entity framework. I have a user control that gets data from an entity model. The user control designer loads fine but the main window throws the following error "Could not create an instance of type 'CampaignList". When i compile the project there is no problem.

I have started using a workaround where i detect when it is in design mode and don't create an "Entities" object when in design mode. However this seems like a bit of an ugly hack. Is there a better way to do this or do we just have to wait for VS to get updated?

+1  A: 

That's exactly what you need to do. It may seem like an ugly hack, heck it may even be an ugly hack, but that's exactly how the framework handles design time rendering. Almost every standard framework control (forms, wpf, and web) determines if its being displayed at runtime or design time and behaves differently. That's the main idea behind component based design. Your classes exist in two different environments--the runtime environment and the design time environment, and they should offer a rich and convenient user experience in both. And the only way to do this is for the control author to code the control to do it.

Optimally, you'll want to handle this situation as high up in the stack as possible. Most definitely you'll want to determine if you're in design time up in your user control and substitute a dummy data source for the entity framework. You'll be displaying the kind of fake data you see in some of the databound forms and wpf controls.

That's the way its done. VS won't ever be "updated" as it works as... well, designed.

Will
A: 

Wow that's crazy. Good to know thats how it has to be done though :)

I guess i have no other choice.

Alex
A: 

Are you trying to load your data in your user control's constructor? If so, yes you will need to check for "Design Mode." If you put the load into some other methodology (such as a specialized "Load" method you call elsewhere in code), the designer can be a lot friendlier.

Alternatively, if you are using a constructor with parameters, you can create a default constructor (no parameters) for the user control that works much friendlier to your designer by not calling loading methods in there.

KP Adrian
A: 

The problem may be that the class (or base class) is defined as abstract. This causes the designer to fail. This problem is described in more detail in the comments section of Laurent Bugnion's blog: http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx

jchadhowell