
MICROSOFT OFFICE DEVELOPMENT
Letting WinForms and WPF controls coexist
Brian Eastwood, Site Editor 06.11.2007
Rating: --- (out of 5)




|
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 ho
To continue reading for free, register below or login
To read more you must become a member of SearchWinDevelopment.com
');
// -->

lding 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:
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.
 |

|
|
 |
|
 |