Your CEO just returned from a Windows Presentation Foundation demo and demands to see some bright, shiny objects in your company's client applications. Trouble is, you have been running WinForms apps for several years, thank you very much, and you surmise that turning a drop-down list into a 3D carousel of images highlighted by drop shadows is far more trouble that it is worth.
Not so fast, Brian Noyes, chief architect at the consultancy IDesign, told developers at Tech Ed 2007. It's rather simple to host WPF controls in a WinForms application, he said, and it's just as easy to do it the other way around.
This sort of interoperability makes sense for several reasons, Noyes continued. The underlying reason is that, though WinForms and WPF controls render differently, underneath both are just components that are .NET class instances.
WinForms is a comprehensive set of controls with a familiar, and mature, designer, while the toolset for building WPF controls is both in its infancy and, at the moment, better suited for designers. At the same time, the video, animation and rich text capabilities of Windows Presentation Foundation are simply not possible with WinForms.
Therefore, Noyes said, a company holding in its hands a huge WinForms app with hundreds of controls will probably want to incorporate WPF controls incrementally. In contrast, an application with only a few WinForms controls is a good candidate for being hosted in Windows Presentation Foundation.
To host WPF in a WinForms app, developers simply need to add to the controls collection the ElementHost, which resides in the System.Windows.FormsIntegration namespace. It is also important to add a reference to the custom control library and a reference to where that control and its base classes live. "It takes about four lines of code to get this stuff going," Noyes said.
The process for hosting WinForms controls in a WPF app is the same, only in this case the host is called the WindowsFormsHost.
Now, this sort of interoperability is not without its challenges, Noyes noted:
- Because the two types of controls are rendered differently, they behave differently. "When a WinForms control is in WPF, it's always going to be on top, and it owns the screen real estate it sits on," he said.
- While WinForms uses physical pixels, Windows Presentation Foundation introduces logical pixels, which take into account the fact that applications may run on screens very large and very small. This means a 100-pixel-by-125-pixel textbox will look different depending on where it was built.
More on WinForms What's new in WinForms 2.0?
Applications: Chapter 14 of "Windows Forms 2.0 Programming"
- The navigation window in WPF actually contains pages, in an effort to mimic the way users are accustomed to browsing in a client application. In contrast, WinForms is used to controlling the lifetime of a window through object references.
- Finally, the focus, styling and opacity of the two types of controls is not 100% consistent.
Ultimately, for the moment it is easier to host WinForms apps in Windows Presentation Foundation that vice versa, Noyes said. At the same time, like all good things WinForms development will eventually come to an end. "The libraries are going to be in the .NET Framework until time eternal, but you're going to want to go to WPF to evolve your application further," he noted.