Preface
Windows Presentation Foundation has been in release since late 2006, as a part of the then .NET 3.0 Framework, also preinstalled on Windows Vista at the time. It promised to change the way rich client applications are written, and eventually replace the old, Win32-based Windows Forms.
WPF gained traction slowly because of its enormous breadth and the different kind of thinking that was required—using XAML, data binding, templates, and styles was very different from the classic WinForms way of working. The power of WPF was evident, but it was difficult to master, and had a steep learning curve.
Over the years things changed; developers started to get used to and appreciate the new way of doing things. XAML began to look convenient and powerful and not just an extra thing to learn with little benefit. Still, for the newcomer, with or without WinForms experience, WPF looks daunting and uncontrollable.
Patterns have emerged, most notably the Model-View-View Model (MVVM), a variant of other existing view-data separation patterns (MVC and MVP), that made life easier (most of the time) but more importantly set a standard way of interaction of view and data; and although many implementations are possible (this is just a pattern, after all), it does let an application be built in more confidence, piece by piece.
This book holds a set of recipes that show how to do common tasks. But don’t just look at the recipes; instead, look at the other sections to deepen your understanding of WPF. No matter the number of recipes, there will always be other things an application needs that no book can cover; by understanding the foundations well, it’s possible to tackle any problem. This is why I have tried to emphasise the why, and not just the how.
WPF led to a bunch of other technologies being built on similar principles, namely Silverlight (cross browser web client development in .NET), Windows Phone 7.x (Microsoft’s Phone OS that uses a Silverlight variant), and lately Windows 8 and Windows Phone 8—all built around similar concepts such as XAML, dependency properties, templates, styles, and bindings—this shows the power and impact of WPF.
What this book covers
Chapter 1, Foundations, introduces the most important concepts in WPF. From the XAML language, to dependency properties, to attached events.
Chapter 2, Resources, discusses WPF’s unique resource system that allows any object to be placed as a resource and consequently shared in an efficient and flexible way.
Chapter 3, Layout and Panels, discusses how WPF manages layout of elements, including looking at the standard layout panels, how they work, and how they can be combined to produce complex and flexible interfaces.
Chapter 4, Using Standard Controls, looks at the major controls in WPF and how they are typically used. The content model is also discussed, along with other control families.
Chapter 5, Application and Windows, takes a look at a WPF application from a higher perspective, including application level resources and the way windows are used and managed.
Chapter 6, Data Binding, discusses the powerful and important concept of data binding and the way it’s used in WPF, including leveraging data templates, converters, and other ideas that make WPF so powerful.
Chapter 7, Commands and MVVM, looks at the way a moderately complex application might be built, by leveraging higher level abstractions known as commands (as opposed to raw events). The MVVM pattern is introduced with some implementation to show how commands, data binding and some extra ingredients can produce a complex, yet manageable, application.
Chapter 8, Styles, Triggers, and Control Templates, shows some of the ways controls can be customized in XAML only, without the need to derive new types for the sake of appearance only.
Chapter 9, Graphics and Animation, provides a tour of the major graphic and animation capabilities of WPF and how they integrate with other mechanisms such as styles and triggers.
Chapter 10, Custom Elements, shows what is required to create custom elements with the considerations that lead to a particular implementation path.
Chapter 11, Threading, discusses WPF’s support for asynchronous operations, so that the UI is responsive at all times, including the support provided in C# 5.0 for performing asynchronous operations more easily.
What you need for this book
The books assumes the reader is a .NET developer working with C# (at least version 2.0, but 3.0 is preferred), and is comfortable working with generics, virtual methods, delegates, and lambdas (C# 3.0). Some WPF exposure is assumed. Visual Studio 2010 as well as Visual Studio 2012 for some features of .NET 4.5.
Who this book is for
The book is intended for developers who are relatively new to WPF, or those who have been working with WPF for a while, but want to a get a deeper understanding of its mechanisms and concepts.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "the typical Window
class is declared as partial
, meaning there may be more source files describing the same class".
A block of code is set as follows:
class Book { public string Name { get; set; } public string Author { get; set; } public decimal Price { get; set; } public int YearPublished { get; set; } }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
<Window x:Class="CH01.CustomTypes.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CH01.CustomTypes"
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "right-click on the project node and select Add and then Class…".
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title through the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.