this print for content only—size & color not accurate spine = 0.802" 344 page count BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Author of Foundations of Atlas PRO ASP.NET 2.0 in VB 2005 PRO ASP.NET 1.1 in VB.NET Web Services Security in the .NET Framework US $39.99 Shelve in .NET User level: Beginner–Intermediate Moroney Foundations of WPF CYAN MAGENTA YELLOW BLACK PANTONE 123 CV ISBN 1-59059-760-5 9 781590 597606 53999 6 89253 59760 6 Companion eBook Available www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version THE APRESS ROADMAP Pro WCF: Practical Microsoft SOA Implementation Pro WF: Windows Workflow in .NET 3.0 Pro WPF: Windows Presentation Foundation in .NET 3.0 Foundations of WPF: An Introduction to Windows Presentation Foundation Foundations of WF: an Introduction to Windows Workflow Foundation Foundations of WPF: An Introduction to Windows Presentation Foundation Dear Reader, 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 application development, allowing developers to take control of the sheer power that is available at the desktop to develop rich, interactive, media-enhanced user interfaces. My first professional programming experience was remodeling a text-based user interface into a Windows-based GUI using Visual Basic 3.0. The productivity enhancements brought by the new language allowed large-scale changes to be made in minutes instead of hours or days. The difference was astonishing. When I look at WPF today, I see the same leap forward. Compared to the Windows GUIs you are used to, you’ll see a huge performance increase in the rich-media GUIs that are now available through WPF. These differences will be clear both to you and to your users. There, on the 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, 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 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 ready to go deeper, arming you with skills for designing, implementing, and deploying applications using WPF. I hope you have as much fun reading this book as I had writing it! Laurence Moroney THE EXPERT’S VOICE ® IN .NET Foundations of WPF An Introduction to Windows Presentation Foundation Get started with the rich new presentation framework from Microsoft. Laurence Moroney 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 orders-ny@springer-sbm.com, 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 info@apress.com, 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.com in 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 ■CHAPTER 3 ■CHAPTER 4 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 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 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 ix ■CONTENTS 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 [...]... Introducing WPF W elcome to Foundations of WPF: An Introduction to Windows Presentation Foundation This book will get you up and running with the framework that is at the heart of the Microsoft Win dows Vista experience If you haven’t yet encountered Windows Presentation Foundation (WPF) , don’t worry— you soon will The experience that it gives from the perspective of users, developers, and designers... is powerful, and before long, just about every application you see will have an element of WPF in it This chapter will bring you up to date and up to speed on web application and user inter face technology from the dawn of computing history to today, give you a context of application architecture and a peek into the future of web application architectures, and show where WPF fits in and how it works... Microsoft in NET 3.0, along with the Windows Communication Foundation and the Windows Workflow Foundation The Windows Presentation Foundation is a vital component to the future of application development, allowing developers to take control of the sheer power that is available at the desktop to develop rich, interactive, media-enhanced user interfaces My first professional programming experience was to. .. companion technologies of Windows Communication Foundation (WCF) and Windows Workflow Foundation (WWF) Presentation Layer Architecture: From Yesterday to Tomorrow The user interface has evolved along a cyclical path from a light footprint to a heavy footprint and back again Users’ requirements and demands for extra functionality drive the heavier foot print, and users’ requirements and demands for an. .. Presentation Foundation (WPF) WinFX introduces a new graphics driver model that is fault tolerant and designed to use the power of the graphics processor unit (GPU) that is on board most modern desktop computers Its memory manager and scheduler enable different graphics applications to use the GPU to run simultaneously Built on top of the GPU and graphics driver is the Windows Graphics Foundation (WGF),... architecture and an XML delivery and design platform Microsoft imple ments the latter methodology using WPF WPF, along with WCF and WWF, forms the basis for how next-generation applications will be built and run on Windows You also went through the suite of tools and SDKs you’ll need and how they all fit together to give you the best development experience possible This book will concentrate on WPF and how... the time of writing, these tools were in beta and as such require some work to install and use If you want to get started developing WPF applications, you’ll need to download and install the various tools, runtimes, and SDKs Visual Studio 2005 The main tool you’ll use is, of course, Visual Studio 2005 This is available in a number of editions: Visual Studio 2005 Team System: This is the top -of- the-line... putting WPF into context by showing how you can use it to build nextgeneration connected and disconnected applications 760-5CH02.qxd 10/6/06 10:37 PM CHAPTER Page 15 2 Programming WPF Applications O ne of the key features of Windows Vista (and other versions of Windows that support WinFX) is the ability to differentiate applications by drastically enhancing the user experience using the Windows Presentation. .. on Long Island, New York, and works for Reuters as a technical specialist He is currently the lead developer of a project to migrate Reuters Messaging to Microsoft Live Communications Server 2005 Prior to that, he spent several years in Reuters’ Microsoft R&D Group and Innovation Lab gaining experience in a broad range of Microsoft products and technologies by participating in many of Microsoft’s beta... application and get into the details of the application-level classes you can use to put together the latest, greatest WPF application What Are WPF Applications? Any application you build using WPF will typically consist of a number of Extensible Application Markup Language (XAML) pages plus their supporting code However, as you can imagine, the functionality that a collection of independent pages can provide . Foundation Foundations of WF: an Introduction to Windows Workflow Foundation Foundations of WPF: An Introduction to Windows Presentation Foundation Dear Reader, The Windows Presentation Foundation is one of the. 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. Practical Microsoft SOA Implementation Pro WF: Windows Workflow in .NET 3.0 Pro WPF: Windows Presentation Foundation in .NET 3.0 Foundations of WPF: An Introduction to Windows Presentation Foundation Foundations