Home > Microsoft .Net Development Tips > Microsoft Office Development > Letting WinForms and WPF controls coexist
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

MICROSOFT OFFICE DEVELOPMENT

Letting WinForms and WPF controls coexist


Brian Eastwood, Site Editor
06.11.2007
Rating: --- (out of 5)


Office Development Channel
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


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.
More on Windows Presentation Foundation
WPF Learning Guide

Introducing WPF: Chapter 1 of Essential Windows Presentation Foundation

Learning .NET: Get started with WPF development

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.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Microsoft Office Development
Check the MSDN Virtual Labs for regular, compelling new content
Windows XP SP3 pack includes new XML parser, enhanced IP security
Book excerpt: Building personalized solutions in SharePoint 2007
Book excerpt: Building document management solutions in SharePoint 2007
XAML as an everywhere presentation format
Where are the Visual Studio 2008 Tools for Office?
ClickOnce: Automated smart client deployment
Book excerpt: SharePoint 2007 and content types
Update desktops automatically using the AppUpdater component
VantagePoint WPF Controls delivers rich suite of UI components

Smart client application development best practices
As some target upstart Silverlight, WinForms and ASP.NET remain strong
VB code download home page
Picking a .NET smart client communications technology
ClickOnce: Automated smart client deployment
Update desktops automatically using the AppUpdater component
Volta aims to ease multi-tier client application development
.NET development in the trenches
Vista Gadgets: Petits Fours for your programming menu
SearchVB.com's Podcast Page
Visual Studio 2008 Learning Guide: Smart client development

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2000 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts