Hosting WPF in WinForms

I’ve done a lot of WPF and Silverlight, but always as standalone apps. In one project, we actually embedded Silverlight inside a WPF application using a browser control.

I’ve been firmly convinced that XAML is the way of the future for some time now. But recently I’ve gotten to see a very real, tangible benefit of hosting WPF inside a windows form application.

A lot of time businesses will want some fancy visualization or way to view their data. In Windows Forms if you want anything “cool” out of the box you’ve got one of these options: paying for a 3rd party set of controls, finding a half-baked control online at CodePlex or CodeProject, or writing it yourself.

Most of the time the project budget and plan don’t include time or money for options 1 and 3. Option 2 is Russian roulette. Sometimes you get some really cool stuff, sometimes the control you find is garbage. In WPF, it becomes almost trivial to do the sort of complex control customization that was such a pain in WinForms. At the same time, most business probably don’t want to start off by rewriting their existing app in WPF.

Using the ElementHost control and some custom WPF UserControls you can embed some very cool UI functionality with minimal effort and impact to your existing application. My WPF UserControls don’t look like “WPF”. At least not all the LOB demos you see where there are fancy transitions and gradients slapped in everywhere. But it’s blowing away the end users. The integration is seamless, it looks like WinForms, but simply using some stylish DataTemplates and the WPF TreeView we’ve improved the customers user experience ten-fold. We’re using it 3 critical places of the app where the user needed some better visualization of the data that was coming in. With WPF we could do that much faster and better with minimal impact to the rest of the application.

2 Replies to “Hosting WPF in WinForms”

  1. Agreed. I was just going to add that the Silverlight runtime is essentially just a COM object and you should be able to host Silverlight directly in an application without the web-browser. There are some goofy things with hosting a web-browser in a WPF/Winforms app with redrawing and Z-Ordering and such. And with the latest (beta still) version of Silverlight it might be even easier.

    WPF FTW!

  2. We all did this in the last project Nick was working on with me. I did use browser control to host silverlight, and did not notice any issues at all. At the end we ended up using DLL import of the IE object because of showing SSRS reports in the same area did not quite work right. But just hosting SL standalone app in browser control is easy. The only annoying part is white background that cannot be surpressed.

Leave a Reply

Your email address will not be published. Required fields are marked *