Tải bản đầy đủ (.pdf) (1,072 trang)

Pro APL in C# 2008 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (15.39 MB, 1,072 trang )

this print for content only—size & color not accurate spine = 2.005" 1,072 page count
Books for professionals By professionals
®
Pro WPF in C# 2008: Windows Presentation
Foundation with .NET 3.5, SECOND EDITION
Dear Reader,
For years, .NET developers have created Windows applications with the
Windows Forms toolkit. And though Windows Forms is mature and reasonably
full-featured, it’s hardwired to essential bits of Windows plumbing that haven’t
changed much in the past ten years. As a result, some tasks that should be easy
require a lot of work.
The Windows Presentation Foundation (WPF) changes all this by introduc-
ing a new model with entirely different plumbing. Although WPF includes the
standard controls you’re familiar with, it draws every detail itself, using the
high-performance DirectX engine. As a result, WPF can provide a whole host
of eye-popping new features, all of which we’ll cover in this book. For example,
we’ll give common controls like buttons a slick, customized look without writ-
ing a line of code. Similarly, we will craft animated effects that rotate, stretch,
scale, and skew anything in your user interface right before the user’s eyes.
Tasks that would have been difficult or nearly impossible in Windows Forms—
such as playing media files or creating texture-mapped 3-D shapes—are finally
possible. By the time you’ve finished this book, you’ll have mastered these pre-
viously impossible tasks.
Although WPF’s video, animation, and 3-D features get the most hype, WPF
is just as useful for building business applications that don’t need rich graphics.
We’ll cover WPF’s enhancements of key business features such as data binding,
print management, and document display, giving you all you need to create
professional business applications in WPF.
Welcome aboard!
Matthew MacDonald (Microsoft MVP, MCSD)
Author of


Pro .NET 2.0 Windows
Forms and Custom Controls
(Apress)
Pro .NET 2.0 Windows
Forms and Custom Controls
in VB 2005 (Apress)
Pro ASP.NET 3.5 in C# 2008
(Apress, with Mario Szpuszta)
Pro WPF: Windows
Presentation Foundation
in .NET 3.0 (Apress)
US $54.99
Shelve in
Programming/Microsoft/.NET
User level:
Intermediate–Advanced
MacDonald
SECOND EDITION
WPF
The eXperT’s Voice
®
in .neT
Pro
WPF in
C# 2008
Windows Presentation Foundation with .NET 3.5
SECOND EDITION
cyan
MaGenTa
yelloW

Black
panTone 123 c
Matthew MacDonald
Companion eBook Available
THE APRESS ROADMAP
Illustrated C# 2008
Beginning C# 2008
Pro LINQ
Pro WPF in C# 2008,
Second Edition
Beginning C# 2008
Databases
Beginning
XML with C# 2008
Beginning VB 2008
Databases
Pro C# 2008 and the .NET
3.5 Platform, Fourth Edition
Pro ASP.NET 3.5 in
C# 2008, Second Edition
www.apress.com
SOURCE CODE ONLINE
Companion eBook

See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-955-6
ISBN-10: 1-59059-955-1
9 781590 599556
5 5 4 9 9

Create the next generation of Windows applications.
Pro
in C#
2008
Matthew MacDonald
Pro WPF in C# 2008
Windows Presentation
Foundation with .NET 3.5
SECOND EDITION
9551CH00 2/8/08 1:40 PM Page i
Pro WPF in C# 2008: Windows Presentation Foundation with .NET 3.5, Second Edition
Copyright © 2008 by Matthew MacDonald
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-0576-0
ISBN-10 (pbk): 1-59059- 955-1
ISBN-13 (electronic): 978-1-59059-955-6
ISBN-10 (electronic): 1-4302-0576-8
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Matt Moodie
Technical Reviewer: Christophe Nasarre
Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell,
Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Sofia Marchant

Copy Editor: Kim Wimpsett
Associate Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Compositor: Diana Van Winkle
Proofreader: Nancy Sixsmith
Indexer: Broccoli Information Management
Artists: Diana Van Winkle, April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit
.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special
Bulk Sales–eBook Licensing web page at />The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any
person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by
the information contained in this work.
The source code for this book is available to readers at .
9551CH00 2/8/08 1:40 PM Page ii
For my wonderful family,
Faria and Maya
9551CH00 2/8/08 1:40 PM Page iii
9551CH00 2/8/08 1:40 PM Page iv
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■CHAPTER 1 Introducing WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
■CHAPTER 3 The Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
■CHAPTER 4 Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
■CHAPTER 5 Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
■CHAPTER 6 Dependency Properties and Routed Events . . . . . . . . . . . . . . . . . . . . . . 137
■CHAPTER 7 Classic Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 8 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
■CHAPTER 9 Pages and Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
■CHAPTER 10 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
■CHAPTER 11 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
■CHAPTER 12 Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
■CHAPTER 13 Shapes, Transforms, and Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
■CHAPTER 14 Geometries, Drawings, and Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
■CHAPTER 15 Control Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
■CHAPTER 16 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
■CHAPTER 17 Data Templates, Data Views, and Data Providers . . . . . . . . . . . . . . . . . 551
■CHAPTER 18 Lists, Trees, Toolbars, and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
■CHAPTER 19 Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
■CHAPTER 20 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
■CHAPTER 21 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
■CHAPTER 22 Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
■CHAPTER 23 3-D Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
■CHAPTER 24 Custom Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
■CHAPTER 25 Interacting with Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903
■CHAPTER 26 Multithreading and Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927

■CHAPTER 27 ClickOnce Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
■INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
v
9551CH00 2/8/08 1:40 PM Page v
9551CH00 2/8/08 1:40 PM Page vi
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■CHAPTER 1 Introducing WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Understanding Windows Graphics
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
DirectX: The New Graphics Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Hardware Acceleration and WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
WPF: A Higher-Level API
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Resolution Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Evolution of WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Windows Forms Lives On
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
DirectX Also Lives On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
The Architecture of WPF
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
■CHAPTER 2 XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Understanding XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Graphical User Interfaces Before WPF . . . . . . . . . . . . . . . . . . . . . . . . 22
The Variants of XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
XAML Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
XAML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
XAML Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The Code-Behind Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Properties and Events in XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Simple Properties and Type Converters . . . . . . . . . . . . . . . . . . . . . . . 31
Complex Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Markup Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Attached Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Nesting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Special Characters and Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . 40
vii
9551CH00 2/8/08 1:40 PM Page vii
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Full Eight Ball Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Using Types from Other Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Loading and Compiling XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Code-Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Code and Uncompiled XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Code and Compiled XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
XAML Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
■CHAPTER 3 The Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
The Application Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Creating an Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Deriving a Custom Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Application Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Application Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Handling Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 64
Accessing the Current Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Interacting Between Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Single-Instance Applications
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
The Last Word
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
■CHAPTER 4
Layout
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Understanding Layout in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The WPF Layout Philosophy
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The Layout Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The Layout Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Simple Layout with the StackPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Layout Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Margin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Minimum, Maximum, and Explicit Sizes . . . . . . . . . . . . . . . . . . . . . . . 84
The WrapPanel and DockPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
The WrapPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
The DockPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Nesting Layout Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Fine-Tuning Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Spanning Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Split Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

■CONTENTSviii
9551CH00 2/8/08 1:40 PM Page viii
Shared Size Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The UniformGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Coordinate-Based Layout with the Canvas . . . . . . . . . . . . . . . . . . . . . . . . 104
Z-Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
The InkCanvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Layout Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
A Column of Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Dynamic Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A Modular User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
■CHAPTER 5 Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Understanding Content Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The Content Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Aligning Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The WPF Content Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Specialized Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
The ScrollViewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
The GroupBox and TabItem: Headered Content Controls . . . . . . . . 127
The Expander . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
The Border
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
The Viewbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
■CHAPTER 6 Dependency Properties and Routed Events . . . . . . . . . . . . . 137
Understanding Dependency Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Defining and Registering a Dependency Property . . . . . . . . . . . . . . 138
How WPF Uses Dependency Properties . . . . . . . . . . . . . . . . . . . . . . 147

Understanding Routed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Defining and Registering a Routed Event . . . . . . . . . . . . . . . . . . . . . 149
Attaching an Event Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Event Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
WPF Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Lifetime Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Input Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Mouse Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
■CONTENTS ix
9551CH00 2/8/08 1:40 PM Page ix
■CHAPTER 7 Classic Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
The Control Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Background and Foreground Brushes . . . . . . . . . . . . . . . . . . . . . . . . 179
Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Mouse Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Content Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Tooltips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Text Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Multiple Lines of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Text Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Miscellaneous TextBox Features
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
The PasswordBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
The ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
The ComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Range-Based Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
The Slider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
The ProgressBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
■CHAPTER 8 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
The Window Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Showing a Window
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Positioning a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Saving and Restoring Window Location . . . . . . . . . . . . . . . . . . . . . . 220
Window Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Window Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
The Dialog Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Common Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Nonrectangular Windows
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
A Simple Shaped Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
A Transparent Window with Shaped Content . . . . . . . . . . . . . . . . . . 231
Moving Shaped Windows
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Resizing Shaped Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Vista-Style Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Using the Windows Vista Glass Effect . . . . . . . . . . . . . . . . . . . . . . . . 236
The Task Dialog and File Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . 241
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
■CONTENTSx
9551CH00 2/8/08 1:40 PM Page x
■CHAPTER 9 Pages and Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Understanding Page-Based Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Page-Based Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

A Simple Page-Based Application with Nav . . . . . . . . . . . . . . . . . . . 247
The Page Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Hosting Pages in a Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Hosting Pages in Another Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Hosting Pages in a Web Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
The Page History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
A Closer Look at URIs in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Navigation History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Maintaining Custom Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
The Navigation Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Programmatic Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Navigation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Managing the Journal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Adding Custom Items to the Journal . . . . . . . . . . . . . . . . . . . . . . . . . 265
Page Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
XAML Browser Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
XBAP Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Creating an XBAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Deploying an XBAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Updating an XBAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
XBAP Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Full-Trust XBAPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Combination XBAP/Stand-Alone Applications
. . . . . . . . . . . . . . . . . 280
Coding for Different Security Levels . . . . . . . . . . . . . . . . . . . . . . . . . 281
Embedding an XBAP in a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . 286
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
■CHAPTER 10 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Understanding Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

The WPF Command Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
The ICommand Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
The RoutedCommand Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
The RoutedUICommand Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
The Command Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Executing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Command Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
■CONTENTS xi
9551CH00 2/8/08 1:40 PM Page xi
Command Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Using Multiple Command Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Fine-Tuning Command Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Invoking a Command Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Disabling Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Controls with Built-in Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Advanced Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Custom Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Using the Same Command in Different Places . . . . . . . . . . . . . . . . 308
Using a Command Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Tracking and Reversing Commands . . . . . . . . . . . . . . . . . . . . . . . . . 310
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
■CHAPTER 11 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Assembly Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Adding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Retrieving Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Pack URIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Content Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Building Localizable User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 324
Preparing an Application for Localization

. . . . . . . . . . . . . . . . . . . . . 325
The Translation Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Object Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
The Resources Collection
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
The Hierarchy of Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Static and Dynamic Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Nonshared Resources
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Accessing Resources in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Application Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
System Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Organizing Resources with Resource Dictionaries . . . . . . . . . . . . . 342
Sharing Resources Between Assemblies . . . . . . . . . . . . . . . . . . . . . 344
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
■CHAPTER 12 Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Style Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Creating a Style Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Setting Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Attaching Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
■CONTENTSxii
9551CH00 2/8/08 1:40 PM Page xii
The Many Layers of Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Automatically Applying Styles by Type . . . . . . . . . . . . . . . . . . . . . . . 359
Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
A Simple Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
An Event Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
■CHAPTER 13 Shapes, Transforms, and Brushes . . . . . . . . . . . . . . . . . . . . . . . . 367
Understanding Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

The Shape Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Rectangle and Ellipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Sizing and Placing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Sizing Shapes Proportionately with a Viewbox . . . . . . . . . . . . . . . . 374
Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Polyline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Line Caps and Line Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Dashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Pixel Snapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Transforming Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Transforming Elements
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Better Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
The LinearGradientBrush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
The RadialGradientBrush
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
The ImageBrush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
A Tiled ImageBrush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
The VisualBrush
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Opacity Masks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Bitmap Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Blurs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Beveled Edges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Embossed Edges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Glows and Shadows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
■CHAPTER 14 Geometries, Drawings, and Visuals . . . . . . . . . . . . . . . . . . . . . . 409

Paths and Geometries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Line, Rectangle, and Ellipse Geometries . . . . . . . . . . . . . . . . . . . . . . 410
Combining Shapes with GeometryGroup . . . . . . . . . . . . . . . . . . . . . 411
Fusing Geometries with CombinedGeometry . . . . . . . . . . . . . . . . . . 413
■CONTENTS xiii
9551CH00 2/8/08 1:40 PM Page xiii
Curves and Lines with PathGeometry . . . . . . . . . . . . . . . . . . . . . . . . 417
The Geometry Mini-Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Clipping with Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Drawings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Displaying a Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Exporting Clip Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Drawing Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Wrapping Visuals in an Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Hit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Complex Hit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
■CHAPTER 15 Control Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Understanding Logical Trees and Visual Trees . . . . . . . . . . . . . . . . . . . . . . 445
Understanding Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
The Chrome Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Dissecting Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Creating Control Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
A Simple Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Template Bindings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Template Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Organizing Template Resources
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465

Refactoring the Button Control Template
. . . . . . . . . . . . . . . . . . . . . 466
Applying Templates with Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Applying Templates Automatically
. . . . . . . . . . . . . . . . . . . . . . . . . . . 470
User-Selected Skins
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Building More Complex Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Multipart Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Control Templates in an ItemsControl . . . . . . . . . . . . . . . . . . . . . . . . 475
Modifying the Scroll Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Creating a Custom Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
The Simple Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
■CHAPTER 16 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Data Binding Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Binding to the Properties of an Element . . . . . . . . . . . . . . . . . . . . . . 491
Creating Bindings with Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Multiple Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
■CONTENTSxiv
9551CH00 2/8/08 1:40 PM Page xiv
Binding Direction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Binding Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Binding to Objects That Aren’t Elements . . . . . . . . . . . . . . . . . . . . . 503
Binding to a Database with Custom Objects . . . . . . . . . . . . . . . . . . . . . . . 507
Building a Data Access Component . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Building a Data Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Displaying the Bound Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Updating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Change Notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514

Binding to a Collection of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Displaying and Editing Collection Items . . . . . . . . . . . . . . . . . . . . . . 516
Inserting and Removing Collection Items . . . . . . . . . . . . . . . . . . . . . 520
Binding to the ADO.NET Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Binding to a LINQ Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Data Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Formatting Strings with a Value Converter . . . . . . . . . . . . . . . . . . . . 527
Creating Objects with a Value Converter . . . . . . . . . . . . . . . . . . . . . 531
Applying Conditional Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Evaluating Multiple Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Validation in the Data Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Custom Validation Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Reacting to Validation Errors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Getting a List of Exceptions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Showing a Different Error Indicator . . . . . . . . . . . . . . . . . . . . . . . . . . 545
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
■CHAPTER 17 Data Templates, Data Views, and Data Providers . . . . . . . 551
Data Binding Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Data Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Separating and Reusing Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 554
More Advanced Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Varying Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Template Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Templates and Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Style Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Changing Item Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Data Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574

Retrieving a View Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Filtering Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Filtering the DataTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
■CONTENTS xv
9551CH00 2/8/08 1:40 PM Page xv
Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Creating Views Declaratively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Navigating with a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
The ObjectDataProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
The XmlDataProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
■CHAPTER 18 Lists, Trees, Toolbars, and Menus . . . . . . . . . . . . . . . . . . . . . . . . 597
The ItemsControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
The ComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
A ListBox with Check Boxes or Radio Buttons . . . . . . . . . . . . . . . . . 604
The ListView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Creating Columns with the GridView . . . . . . . . . . . . . . . . . . . . . . . . . 608
Resizing Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Cell Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Creating a Custom View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
The TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
A Data-Bound TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Binding a DataSet to a TreeView
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
Just-in-Time Node Creation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
The Menu Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

Menu Items
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
The ContextMenu Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Menu Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Toolbars and Status Bars
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
The ToolBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
The StatusBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
■CHAPTER 19 Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Understanding Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Flow Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
The Flow Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Formatting Content Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Constructing a Simple Flow Document . . . . . . . . . . . . . . . . . . . . . . . 648
Block Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Inline Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
Interacting with Elements Programmatically . . . . . . . . . . . . . . . . . . 663
■CONTENTSxvi
9551CH00 2/8/08 1:40 PM Page xvi
Text Justification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Read-Only Flow Document Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Zooming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Pages and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Loading Documents from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Editing a Flow Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
Loading a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
Saving a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
Formatting Selected Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678

Getting Individual Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Fixed Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
The Annotation Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Enabling the Annotation Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
Creating Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Examining Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Reacting to Annotation Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
Storing Annotations in a Fixed Document . . . . . . . . . . . . . . . . . . . . . 694
Customizing the Appearance of Sticky Notes . . . . . . . . . . . . . . . . . 695
Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
■CHAPTER 20
Printing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Basic Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Printing an Element
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
Transforming Printed Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Printing Elements Without Showing Them . . . . . . . . . . . . . . . . . . . . 703
Printing a Document
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Manipulating the Pages in a Document Printout . . . . . . . . . . . . . . . 708
Custom Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
Printing with the Visual Layer Classes . . . . . . . . . . . . . . . . . . . . . . . 711
Custom Printing with Multiple Pages . . . . . . . . . . . . . . . . . . . . . . . . 714
Print Settings and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Maintaining Print Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Printing Page Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Managing a Print Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Printing Through XPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724

Creating an XPS Document for a Print Preview . . . . . . . . . . . . . . . . 725
Printing Directly to the Printer via XPS . . . . . . . . . . . . . . . . . . . . . . . 726
Asynchronous Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
■CONTENTS xvii
9551CH00 2/8/08 1:40 PM Page xvii
■CHAPTER 21 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Understanding WPF Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Timer-Based Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Property-Based Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
Basic Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
The Animation Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
Animations in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
Simultaneous Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Animation Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
The Timeline Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Declarative Animation and Storyboards . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
The Storyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
Event Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
Overlapping Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
Simultaneous Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Controlling Playback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
Monitoring Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Desired Frame Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Animation Types Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
Animating Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Animating Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
Key Frame Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
Path-Based Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Frame-Based Animation

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
■CHAPTER 22 Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Playing WAV Audio
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
The SoundPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
The SoundPlayerAction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
System Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
The MediaPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
The MediaElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
Playing Audio Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
Playing Audio with Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
Playing Multiple Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
Changing Volume, Balance, Speed, and Position . . . . . . . . . . . . . . 795
Synchronizing an Animation with Audio . . . . . . . . . . . . . . . . . . . . . . 797
Playing Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
Video Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
■CONTENTSxviii
9551CH00 2/8/08 1:40 PM Page xviii
Speech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Speech Synthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Speech Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
■CHAPTER 23 3-D Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
3-D Drawing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
The Viewport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
3-D Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
The Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
Deeper into 3-D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823

Shading and Normals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
More Complex Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Model3DGroup Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Materials Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
Interactivity and Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838
Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838
Rotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840
A Fly Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
The Trackball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
Hit Testing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
2-D Elements on 3-D Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
■CHAPTER 24 Custom Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Understanding Custom Elements in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . 856
Building a Basic User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Defining Dependency Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
Defining Routed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
Adding Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Using the Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866
Command Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866
A Closer Look at User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
Lookless Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870
Refactoring the Color Picker Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
Refactoring the Color Picker Markup . . . . . . . . . . . . . . . . . . . . . . . . 871
Streamlining the Control Template . . . . . . . . . . . . . . . . . . . . . . . . . . 874
Theme-Specific Styles and the Default Style . . . . . . . . . . . . . . . . . . 876
Extending an Existing Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
Understanding Masked Edit Controls . . . . . . . . . . . . . . . . . . . . . . . . 879

■CONTENTS xix
9551CH00 2/8/08 1:40 PM Page xix
Mask Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
The MaskedTextProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Implementing a WPF Masked Text Box . . . . . . . . . . . . . . . . . . . . . . . 881
Improving the MaskedTextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Custom Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
The Two-Step Layout Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
The Canvas Clone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891
A Better Wrapping Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892
Custom-Drawn Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
The OnRender() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896
Evaluating Custom Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897
A Custom-Drawn Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
A Custom Decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902
■CHAPTER 25 Interacting with Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . 903
Assessing Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903
Missing Features in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904
Mixing Windows and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Adding Forms to a WPF Application . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Adding WPF Windows to a Windows Forms Application
. . . . . . . . 907
Showing Modal Windows and Forms
. . . . . . . . . . . . . . . . . . . . . . . . 907
Showing Modeless Windows and Forms . . . . . . . . . . . . . . . . . . . . . 908
Visual Styles for Windows Forms Controls
. . . . . . . . . . . . . . . . . . . . 909
Windows Forms Classes That Don’t Need Interoperability
. . . . . . 909

Creating Windows with Mixed Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . 914
WPF and Windows Forms “Airspace”
. . . . . . . . . . . . . . . . . . . . . . . . 914
Hosting Windows Forms Controls in WPF
. . . . . . . . . . . . . . . . . . . . . 916
WPF and Windows Forms User Controls . . . . . . . . . . . . . . . . . . . . . . 918
Hosting WPF Controls in Windows Forms . . . . . . . . . . . . . . . . . . . . . 919
Access Keys, Mnemonics, and Focus . . . . . . . . . . . . . . . . . . . . . . . . 921
Property Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
■CHAPTER 26 Multithreading and Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
The Dispatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
The DispatcherObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
The BackgroundWorker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
Application Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
The Add-in Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
■CONTENTSxx
9551CH00 2/8/08 1:40 PM Page xx
An Application That Uses Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
Interacting with the Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
Visual Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
■CHAPTER 27 ClickOnce Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
Application Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
Understanding ClickOnce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
The ClickOnce Installation Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
ClickOnce Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
A Simple ClickOnce Publication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
Choosing a Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970

Deployed Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
Installing a ClickOnce Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
Updating a ClickOnce Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
ClickOnce Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
Publish Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
Publish Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 980
The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
■CONTENTS xxi
9551CH00 2/8/08 1:40 PM Page xxi
About the Author
■MATTHEW MACDONALD is an author, educator, and Microsoft MVP in Windows client
development. He’s a regular contributor to programming journals and the author of more
than a dozen books about .NET programming, including Pro .NET 2.0 Windows Forms and
Custom Controls in C# (Apress, 2005) and Pro ASP.NET 3.5 in C# 2008 (Apress, 2007). He lives
in Toronto with his wife and daughter.
xxii
9551CH00 2/8/08 1:40 PM Page xxii
About the Technical Reviewer
■CHRISTOPHE NASARRE is a software architect and development lead for
Business Objects, a multinational software company focused on business
intelligence solutions. During his spare time, Christophe writes articles for
MSDN Magazine, MSDN, and ASPToday. Since 1996, he has also worked as
a technical editor on numerous books on Win32, COM, MFC, .NET, and
WPF. In 2007, he wrote his first book, Windows via C/C++ from MSPress.
xxiii
9551CH00 2/8/08 1:40 PM Page xxiii
4e39d27715ea33bfeed83c26800166a2

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×