Problem solve Get help with specific problems with your technologies, process and projects.

Beginning Windows Presentation Foundation development, Part 2

This tip revisits Windows Presentation Foundation with a look at custom controls, WinForms interoperability, XAML, data binding and more.

Some Windows Presentation Foundation news has been made since penned its first Beginning WPF development tip in November 2006 -- enough news that it seemed prudent to produce a second tip.

For starters, the tips, videos and additional WPF learning content that Microsoft has assembled have all moved to WindowsClient.NET, a site that, as its name suggests, focuses on client applications in the .NET Framework 3.0. This new site was launched at Tech Ed 2007 and coincided with the announcement of Acropolis, a set of components and other tools for use with both Windows Presentation Foundation and WinForms.

Developers may think they have to choose either WinForms or WPF controls, but, in fact, it is possible for both types of controls to be in the same application, thereby eliminating the need for a large-scale application migration. At Tech Ed 2007, Brian Noyes explained how to work with the two frameworks; the story Letting WinForms and WPF controls coexist has the details.

When it comes to considering Windows Presentation Foundation controls, it is a good idea to look inside the proverbial box first. There are numerous controls for buttons, TextBoxes, graphics and so on baked right into WPF. Inevitably, though, someone -- a graphic designer, the CEO, Bob from accounting -- will want a custom control.

In such cases, there are a few things to keep in mind, Shaun Wildermuth notes in the MSDN article Customizing Controls for Windows Presentation Foundation.

More on WPF development
Introducing WPF: Chapter 1 of Essential Windows Presentation Foundation

Using WPF to give .NET applications that missing something

If all you need to do is change the appearance of an out-of-the-box control, then styles and templates will do, Wildermuth says. If, however, you need to go beyond that, then there are two options. User controls are made of existing components and, thus, do not need complex customization. Custom controls, on the other hand, give developers full control over appearance and rendering. When in doubt, he suggests, go with a user control.

If talk of WPF controls flies over your head just a bit, and you are looking for more of an overview of Windows Presentation Foundation, then you could do worse that check out the recent CodeProject tip series, A Guided Tour of WPF. This is a five-part series by Josh Smith, a developer currently working on the New York Times Reader WPF-based application.

Part 1 focuses on XAML, the Extensible Application Markup Language that sits in code-behind files and makes UI styling quite a bit easier in Windows Presentation Foundation. It also introduces the admittedly silly horse race application that serves as the sample throughout the series.

Part 2 looks at the layout panel, a concept familiar to Web applications but hitherto absent from traditional client applications. Because this layout system is so extensible, the architects of WPF introduced the concept of attached properties. This, Smith notes, will "set a layout property on a visual element in a panel without that element ever knowing about the panel or its properties."

Part 3 offers some thoughts on data binding in Windows Presentation Foundation, which is flexible enough that developers can, with the use of one API, bind to business objects, XML, ADO.NET data containers, collections and so on. This is possible because WPF introduces dependency properties; these can be bound, can be set in XAML and behave similarly to WinForms' ambient properties, Smith says.

Part 4 considers data templates and triggers. The former are one of many types of templates in WPF; others include control templates, items panel templates and hierarchical data templates. Triggers, meanwhile, conditionally apply values to properties within a data template, such as changing the color of a block of text from black to red when a cursor is hovering over it.

Part 5 discusses styles in Windows Presentation Foundation, which are quite similar to Cascading Style Sheets in the wide world of Web development. In WPF, a style object can be applied to a specific type of UI element; it "really just boils down to a convenient way to apply property settings to one or more visual elements," Smith says.

With Visual Studio 2008 (formerly Visual Studio Orcas) set for release by the end of 2007 -- yes, that's correct -- we expect additional tips and tricks for Windows Presentation Foundation development to continue to emerge. Stay tuned for more, and feel free to send us your favorite WPF resources.

Dig Deeper on Windows Presentation Foundation

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.