Table Of ContentCYAN YELLOW
MAGENTA BLACK
PANTONE 123 CV
BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN.NET
Companion
eBook Available
Foundations of WPF: An Introduction to Windows
Presentation Foundation
F
o
Dear Reader,
u
The Windows Presentation Foundation is one of the new “foundations” being
introduced by Microsoft in .NET 3.0. WPF is a vital component to the future of n Foundations of
application development, allowing developers to take control of the sheer power
d
that is available at the desktop to develop rich, interactive, media-enhanced
user interfaces. a
Author of WPF
My first professional programming experience was remodeling a text-based t
Foundations of Atlas
user interface into a Windows-based GUI using Visual Basic 3.0. The productivity i
PRO ASP.NET 2.0 in VB 2005 enhancements brought by the new language allowed large-scale changes to be o
PRO ASP.NET 1.1 in VB.NET made in minutes instead of hours or days. The difference was astonishing. n
Web Services Security When I look at WPF today, I see the same leap forward. Compared to the s
in the.NET Framework Windows GUIs you are used to, you’ll see a huge performance increase in the
rich-media GUIs that are now available through WPF.
o
These differences will be clear both to you and to your users. There, on the
f
screen, will be fast, responsive applications that take full advantage of the
graphics hardware of the machines on which they run; this will provide a richer, W
more immersive experience than you have ever experienced. Furthermore,
you’ll achieve these miracles quickly and simply from within a comprehensive
development environment.
WPF has a huge, varied application programming interface (API) that aims
P
to make even the most complex tasks straightforward, and this book gives you
the introduction you need to get started with it. In addition, this book gets you
An Introduction to Windows Presentation Foundation
ready to go deeper, arming you with skills for designing, implementing, and
F
deploying applications using WPF.
I hope you have as much fun reading this book as I had writing it!
Laurence Moroney
Companion eBook THE APRESS ROADMAP Get started with the rich new presentation framework from Microsoft.
Pro WPF: Windows
Presentation Foundation
Foundations of WF: in .NET 3.0
an Introduction to
Windows Workflow Foundation
Pro WCF: Practical
See last page for details Microsoft SOA Implementation
Foundations of WPF:
on $10 eBook version
An Introduction to Windows
Presentation Foundation
Pro WF: Windows
Workflow in .NET 3.0
M Laurence Moroney
SOURCECODEONLINE o
r
www.apress.com ISBN 1-59059-760-5 o
n
53999 e
y
US $39.99
Shelve in
.NET
User level:
Beginner–Intermediate 6 89253 59760 6 9 781590 597606
this print for content only—size & color not accurate spine = 0.802" 344 page count
760-5FM.qxd 10/25/06 4:07 PM Page i
Foundations of WPF
An Introduction to Windows
Presentation Foundation
Laurence Moroney
760-5FM.qxd 10/25/06 4:07 PM Page ii
Foundations of WPF: An Introduction to Windows Presentation Foundation
Copyright © 2006 by Laurence Moroney
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-59059-760-6
ISBN-10 (pbk): 1-59059-760-5
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.
Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the
US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was writ-
ten without endorsement from Sun Microsystems, Inc.
Lead Editor: Ewan Buckingham
Technical Reviewer: John Grieb
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser,
Keir Thomas, Matt Wade
Project Manager: Denise Santoro Lincoln
Copy Edit Manager: Nicole Flores
Copy Editor: Kim Wimpsett
Assistant Production Director: Kari Brooks-Copony
Production Editor: Ellie Fountain
Compositor: Gina Rexrode
Proofreader: Linda Marousek
Indexer: Michael Brinkman
Artist: Kinetic Publishing Services, LLC
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 [email protected], or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com.
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 http://www.apress.comin the Source Code/
Download section.
760-5FM.qxd 10/25/06 4:07 PM Page iii
I dedicate this book to my wife,Rebecca,and my wonderful children,Claudia and
Christopher—you are always an inspiration to me and are the greatest blessing
in the world.Thank you for everything!
760-5FM.qxd 10/25/06 4:07 PM Page iv
760-5FM.qxd 10/25/06 4:07 PM Page v
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■ CHAPTER 1 Introducing WPF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■ CHAPTER 2 Programming WPF Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
■ CHAPTER 3 Building Your First WPF Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
■ CHAPTER 4 Building a Connected WPF Application. . . . . . . . . . . . . . . . . . . . . . . . . . 71
■ CHAPTER 5 Exploring the Layout Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
■ CHAPTER 6 Working with XAML Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
■ CHAPTER 7 Working with Graphics and Media. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
■ CHAPTER 8 Working with Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
■ CHAPTER 9 Working with 3D Graphics in WPF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
■ CHAPTER 10 Distributing Your WPF Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
v
760-5FM.qxd 10/25/06 4:07 PM Page vi
760-5FM.qxd 10/25/06 4:07 PM Page vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■ CHAPTER 1 Introducing WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Presentation Layer Architecture:From Yesterday to Tomorrow. . . . . . . . . . 1
The Windows SDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
The .NET Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Windows Communication Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Windows Workflow Foundation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Windows Presentation Foundation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Tools of the Trade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Visual Studio 2005. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Microsoft Expression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The WinFX Runtime Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The Windows Software Development Kit (SDK). . . . . . . . . . . . . . . . . 14
Visual Studio Development Tools for WinFX . . . . . . . . . . . . . . . . . . . . 14
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
■ CHAPTER 2 Programming WPF Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
What Are WPF Applications?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Choices in Application Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Choosing a XAML Browser Application . . . . . . . . . . . . . . . . . . . . . . . . 17
Choosing an Installed Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Windows Presentation Foundation Architecture. . . . . . . . . . . . . . . . . . . . . . 19
System.Threading.DispatcherObject . . . . . . . . . . . . . . . . . . . . . . . . . . 20
System.Windows.DependencyObject. . . . . . . . . . . . . . . . . . . . . . . . . . 20
System.Windows.Media.Visual. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
System.Windows.UIElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
System.Windows.FrameworkElement. . . . . . . . . . . . . . . . . . . . . . . . . 23
vii
760-5FM.qxd 10/25/06 4:07 PM Page viii
viii ■ CONTENTS
How to Program WPF Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Using the Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Supporting Application-Level Navigation Events . . . . . . . . . . . . . . . . 28
Using the Application Object to Manage Your Windows . . . . . . . . . . 29
Managing the Shutdown of Your Application. . . . . . . . . . . . . . . . . . . . 29
Window Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Using the Window Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Using the NavigationWindow Object. . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Using the Page Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Managing Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
■ CHAPTER 3 Building Your First WPF Application. . . . . . . . . . . . . . . . . . . . . . . 33
Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Building an Application in Expression Interactive Designer. . . . . . . . . . . . 36
Creating the Basic Application Layout. . . . . . . . . . . . . . . . . . . . . . . . . 36
Performing the Basic Data Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Running the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Exploring the XAML for the Bike Browser. . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Document Root:The Grid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Scene Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Triggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
User Interface Definition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
A More Advanced WPF User Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Setting the Background Gradient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Creating the 3D “Gel”Effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Creating a Reflection Effect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
■ CHAPTER 4 Building a Connected WPF Application. . . . . . . . . . . . . . . . . . . . 71
Getting Started with WCF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Creating the Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Creating the WCF Service Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Creating the WCF Service Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Adding the Database Wrapper. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Adding the Service Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Creating the Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Connecting the User Interface to the Service. . . . . . . . . . . . . . . . . . . 93
760-5FM.qxd 10/25/06 4:07 PM Page ix
■ CONTENTS ix
Implementing the Product Detail Service Method. . . . . . . . . . . . . . . 99
Implementing the User Interface for the Product Service Call. . . . 102
Tidying Up the XAML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
■ CHAPTER 5 Exploring the Layout Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Understanding Layout Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Using the StackPanel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Getting a StackPanel Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Docking a StackPanel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Using Margins in a StackPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Understanding When You Would Use a StackPanel . . . . . . . . . . . . . 121
Using the DockPanel Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Using Child Fills. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Using the Grid Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Using Columns and Rows in the Grid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Building the UI with Columns and Rows. . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Using the Canvas Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Using the TabPanel for Tabbed Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Using the WrapPanel Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Setting the Wrap Flow Direction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Setting the Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
■ CHAPTER 6 Working with XAML Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Using the Button Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Getting Started with the Button Control. . . . . . . . . . . . . . . . . . . . . . . 152
Using Rich Content in a Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Using the CheckBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Getting Started with the CheckBox Control. . . . . . . . . . . . . . . . . . . . 155
Using Rich Content in a CheckBox Control . . . . . . . . . . . . . . . . . . . . 155
Using the ComboBox Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Getting Started with the ComboBox Control. . . . . . . . . . . . . . . . . . . 157
Using Rich Content in a Combo Box. . . . . . . . . . . . . . . . . . . . . . . . . . 159
Using the Slider Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Getting Started with the Slider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Using Tick Marks for Improved Feedback. . . . . . . . . . . . . . . . . . . . . 161
Using a Selection Range in a Slider . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Capturing Value Changes on the Slider . . . . . . . . . . . . . . . . . . . . . . . 163