Dude, Where's My Control?

I'm blogging to:

Johnny Cash
The Legend of Johnny Cash

I have received numerous emails from people who are using the Feb CTP version of Crossbow and have run into the bug that we have in ElementHost where the hosted WPF content does not show up until you resize the Form that contains the ElementHost control.

While this may be a small enough issue, many people have requested a work-around for the problem.  Well, you would think that all you have to do is force a repaint by calling Invalidate() or Refresh() on the Form, but alas this does not make the content appear.  So how can we fix this?

Don't fret my little Crossbow consumers...Uncle Mike is here to hook you up.  Here's what you need to do...At the end of your Form_Load() event handler, hook the Shown event and change the Form's size.  So, something like:

private void Form1_Load(object sender, EventArgs e) {
     ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
     UserControl1 ctrl = new UserControl1();
host.Controls.Add(ctrl);
     this.Controls.Add(host);

     // Here's the magic line of code
     Shown += delegate { Width++; };
}

This little hack will at least get you up and running so that you can see your hosted WPF content when the Form loads.  Generally, this will suffice until you get the version of Crossbow that has this fixed.