+1  A: 

You have a bug in the constructor of the usercontrol - you are using a foreach-loop over an IEnumerable and while the loop is running, the IEnumerable is changed, this is not allowed with a foreach loop. Use a for loop instead if you are manipulating the Collection you are iterating over.

Femaref
I'm not using a for loop to add the control. I've got a different control that is working just fine. I'm able to add multiple instances of that controlThis is a single control that is in the toolbox that I'm just trying to drag to form.
Codejnki
I didn't say you are using a loop to add the control, I said you are using a loop in the constructor of that usercontrol. the ctor gets called after you dragged it into the designer.
Femaref
Thanks, sorry about that. Got it figured out. Needed a constructor with no arguments and then it worked.
Codejnki
A: 

The problem here is that you don't know what code is throwing the exception.

WPF is terrible about exceptions, especially in constructors. The framework insists on catching and re-throwing a new exception, usually multiple times, and it's difficult to find the original stack trace. I've found the easiest way to track down this kind of error is to tell Visual Studio to stop as soon as the exception is thrown, rather than waiting until WPF has re-thrown it a couple of times and made the details difficult to dig out.

I don't have Visual Studio 2010 in front of me, but here's how to do this in VS2008 -- 2010 is probably similar:

  • Go to the Debug menu > Exceptions...
  • Next to "Common Runtime Language Exceptions", check the box in the "Thrown" column

Then debug your app again. It will stop at the line that's actually causing the problem, and it'll be much easier for you to see what's going on. And if you're still not sure why it's throwing an exception, you'll be able to post a code sample.

Joe White
The error is being thrown by VS. Something tells me I'm missing a critical step. The control is part of my project, part of the same namespace. The editor is complaining about it big time. Just as a test I added a Stack Panel to the main form and went to the codebehind, and I was able to add the control to stack panel without incident.
Codejnki
You can always use VS to debug another instance of VS. Or start commenting out your code until it stops giving you the error.
Joe White
I think I figured it out. I went to the XAML code and tried manually entering in my control using <my:TestControl>. The next build then failed because the control didn't have a constructor that took zero arguments. I modified me control so that the constructor had zero arguments it worked just fine.
Codejnki
A: 

In order for a user control to function properly you need to have a constructor that takes zero arguments. This way the form designer has a way to render the control in a "default" manner.

I then overloaded my constructor to take the arguments I needed to actually run the control properly and everything worked as expected.

Codejnki