Book Review - Programming Windows Presentation Foundation

Cover: Programming Windows Presentation Foundation

Authors: Chris Sells & Ian Griffiths

Publisher: O'Reilly

ISBN: 0-596-10113-9

buy from Amazon

 

Overview: Chris Sells and Ian Griffiths, experts and authors in the Windows Forms arena (and before that MFC and ATL) have written the best WPF book on the market*. At 420 pages the book is slimmer than the authors’ past forays into Windows Forms, but still does a great job of covering the main aspects of WPF. The book was written against the beta 1 version of WPF, and there have been substantial changes to some parts of WPF since it was written. Fortunately Chris Sells is maintaining a list of the changes here. A second edition of the book against the RTM version of WPF is planned for later this year. As the first book on WPF on the market it has set a high standard which other books will have to meet. This book is a great way to get started learning WPF.

 

Chapter 1: Overview of WPF – I don’t quite know how they do it but Chris and Ian manage to cover off all the basics of WPF, from writing WPF in C# to XAML, layout, content model, data binding and deployment.

 

Chapter 2: Layout – A good overview of elements for controlling layout (various kinds of panels), as well as text layout. I would have liked to see more Grid hacks in this chapter.

 

Chapter 3: Controls – Explains the WPF eventing system (routed events) and the types of events the various input devices can generate for controls. This chapter then explains Commands (a concept that is often used but now has built-in support in WPF), before going on to cover the basics of many of the built-in controls. Because of the number of controls the treatment given to each is fairly focused on “what makes this thing different” rather than listing off every single property and method.

 

Chapter 4: Data Binding – This chapter gives good coverage to one of the cooler features in WPF – data binding. It begins with an application where we “roll our own” data binding code to synchronize changes between a number of input and display controls. After showing how complex this can become (even with a relatively small number of controls) we are relieved to discover that WPF does it all for us with concise syntax. The examples show binding to objects and Xml documents (but unfortunately not to relational data because the WPF support for it at the time of writing was immature). We learn about binding to lists of data, sorting, filtering, asynchronous data retrieval and master-detail binding. Well worth reading.

 

Chapter 5: Styles and Control Templates – Shows how styles (similar to HTML styles, but defined in XAML) can be used to customize the look of controls. Triggers which allow the look of a control to be changed when a property has a certain value are explained for both controls and data. This chapter also shows how data can be “styled” in the same way that controls can, and finally how the entire rendering for a control can be replaced with a Control Template. I thought the coverage of some of these topics could have been expanded. For example the section on control templating could probably cover an entire chapter. This chapter is available on-line here.

 

Chapter 6: Resources – This chapter explains what resources are (things like brushes, styles and images are things that you might want to define in a single place and re-use throughout your app via resources), how they are scoped and the different APIs for accessing them. This chapter then goes on to discuss skinning and themeing using resources, before moving on to localizing your WPF app.

 

Chapter 7: Graphics – This chapter begins with an overview of some of the fundamentals of the WPF graphics system, answering important questions like “what IS a logical pixel anyway?” and “how can my button be 20.5 pixels wide?”. This lays the foundations for discussions on graphics primitives like shapes, brushes, pens, rectangles, ellipses, lines, polygons and finally the all-powerful path (which can do all the things the others can do). Discussion of brushes covers solid colour, linear and radial gradient brushes (WPF programs should have gradients on every control) before moving on to the more esoteric ImageBrush, DrawingBrush and VisualBrush. VisualBrush is useful for painting something with the contents of some other part of your UI, making it useful for magnification and reflections. Transformations are shown, as well as an introduction to the lower-level Visual-Layer API. Video and 3D are given a brief mention and code sample also. This chapter had a lot of ground to cover – the WPF graphics system could probably fill a whole book – and some of the topics such as the difference between the Viewbox and Viewport will probably be fairly boring on the first read, but this is still an important chapter.

 

Chapter 8: Animation – Describes the new animation system in WPF beginning with important introductory concepts such as “what properties can be animated?” before moving on to cover timelines and their important properties, and how they can be nested, storyboards (which group animations together), key frames (where “important points in time” can be defined and WPF will manage moving between those important points in an appropriate manner), and creating and calling animations procedurally. This chapter has one of my favorite lines from the book “When a nested timeline with a RepeatBehavior of Forever reaches the end of its duration, it goes back to the start and continues to repeat until the end of time, but only for small values of ‘the end of time’.”

 

Chapter 9: Custom Controls – Although controls will probably end up being less important in WPF than they have been in legacy UI technologies (due to the great things that can be done with templating and styling) this chapter does a good job of describing the process of building a control from scratch, deciding what class to inherit from, exposing attached properties and routed events and providing visuals that can be overridden in the WPF way.

 

Chapter 10: ClickOnce Deployment – This chapter does a workman-like job of describing the ins and outs of deploying WPF applications with ClickOnce. Apart from describing the ability for WPF applications to be hosted in the browser most of the material here is not specific to WPF.

 

Appendix A: XAML – Its hard to gauge how important it will be to have a full understanding of XAML going forward (since tools may remove the need for designers and developers to know it), but at this stage (early 2006) the tool support is not good enough to remove the need to know it altogether. Reading the book will give you a good understanding of the basics of XAML, but this chapter goes further – explicitly explaining the mechanisms behind things like Markup Extensions, showing how to alias CLR namespaces to XML namespaces in XAML and other interesting topics. Its probably desirable that you read this appendix after reading the first couple of chapters of the book to remove uncertainty about how XAML works.

 

Appendix B: Interoperability – This appendix discusses what happened to the HWNDs in WPF (they went away) and how you can bring them back if you need to. This allows WPF to host HWND-based UI controls (MFC, WindowsForms) and allow WPF controls to be hosted in MFC and Windows Forms, and the limitations imposed on said hosting.

 

Appendix C: Asynchronous and Multithreaded Programming in WPF Applications – The need to care about which thread is doing updates to UI does not go away in WPF. This Chapter discusses the Dispatcher which can be used to jump onto the right thread, as well as using the BackgroundWorker to do updates on the correct thread.

 

* At this stage it is the only WPF book on the market.