Pro WPF - Springer

7 downloads 1884 Views 135KB Size Report
Pro WPF: Windows Presentation Foundation in .NET 3.0. Copyright © 2007 ... Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1. Trademarked  ...
Pro WPF Windows Presentation Foundation in .NET 3.0

Matthew MacDonald

Pro WPF: Windows Presentation Foundation in .NET 3.0 Copyright © 2007 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-59059-782-8 ISBN-10 (pbk): 1-59059-782-6 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: Ewan Buckingham Technical Reviewer: Christophe Nasarre Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, Chris Mills, Matthew Moodie, Jeff Pepper, Dominic Shakeshaft, Matt Wade Project Manager: Tracy Brown Collins Copy Edit Manager: Nicole Flores Copy Editors: Jennifer Whipple, Kim Wimpsett Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Cheu Compositor and Artist: Diana Van Winkle, Van Winkle Design Proofreader: Lori Bring Indexer: Toma Mulligan 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 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. 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.com in the Source Code/ Download section and at http://www.prosetech.com.

For my wonderful family, Faria and Maya

Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

■CHAPTER 1 Introducing WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 ■CHAPTER 3 The Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 ■CHAPTER 4 Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 ■CHAPTER 5 Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 ■CHAPTER 6 Dependency Properties and Routed Events . . . . . . . . . . . . . . . . . . . 143 ■CHAPTER 7 Classic Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 ■CHAPTER 8 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 ■CHAPTER 9 Pages and Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 ■CHAPTER 10 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 ■CHAPTER 11 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 ■CHAPTER 12 Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 ■CHAPTER 13 Shapes, Transforms, and Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 ■CHAPTER 14 Geometries, Drawings, and Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 ■CHAPTER 15 Control Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 ■CHAPTER 16 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 ■CHAPTER 17 Data Templates, Data Views, and Data Providers . . . . . . . . . . . . . . 547 ■CHAPTER 18 Lists, Trees, Toolbars, and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593 ■CHAPTER 19 Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 ■CHAPTER 20 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 ■CHAPTER 21 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721 ■CHAPTER 22 Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775 ■CHAPTER 23 3-D Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801 ■CHAPTER 24 Custom Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 ■CHAPTER 25 Interacting with Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891 ■CHAPTER 26 ClickOnce Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933

v

Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

■CHAPTER 1

Introducing WPF

.............................................1

Understanding Windows Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 WPF: A Higher-Level API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 The Architecture of WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

■CHAPTER 2

XAML

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Understanding XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 XAML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Properties and Events in XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Using Types from Other Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Loading and Compiling XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

■CHAPTER 3

The Application

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Creating an Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Deriving a Custom Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

■CHAPTER 4

Layout

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

Understanding Layout in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Simple Layout with the StackPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 The WrapPanel and DockPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 The Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Coordinate-Based Layout with the Canvas . . . . . . . . . . . . . . . . . . . . . . . . 111 Layout Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

vi

■CONTENTS

■CHAPTER 5

Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Understanding Content Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Specialized Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

■CHAPTER 6

Dependency Properties and Routed Events

. . . . . . . . . . . . . 143

Understanding Dependency Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Understanding Routed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 WPF Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

■CHAPTER 7

Classic Controls

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

The Control Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Content Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Text Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Range-Based Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

■CHAPTER 8

Windows

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

The Window Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Window Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Nonrectangular Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Vista-Style Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

■CHAPTER 9

Pages and Navigation

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

Understanding Page-Based Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Page-Based Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 The Page History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The Navigation Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 XAML Browser Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

■CHAPTER 10 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Understanding Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 The WPF Command Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Executing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Advanced Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

vii

viii

■CONTENTS

■CHAPTER 11 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Assembly Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Object Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

■CHAPTER 12 Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Style Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

■CHAPTER 13 Shapes, Transforms, and Brushes . . . . . . . . . . . . . . . . . . . . . . . . 371 Understanding Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 Better Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 Bitmap Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

■CHAPTER 14 Geometries, Drawings, and Visuals . . . . . . . . . . . . . . . . . . . . . . 413 Paths and Geometries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 Drawings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

■CHAPTER 15 Control Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Understanding Logical Trees and Visual Trees . . . . . . . . . . . . . . . . . . . . . . 449 Understanding Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Creating Control Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 Organizing Template Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 Building More Complex Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492

■CHAPTER 16 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Data Binding Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Binding to a Database with Custom Objects . . . . . . . . . . . . . . . . . . . . . . . 509 Binding to a Collection of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 Data Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545

■CONTENTS

■CHAPTER 17 Data Templates, Data Views, and Data Providers . . . . . . . 547 Data Binding Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 Data Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Data Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570 Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592

■CHAPTER 18 Lists, Trees, Toolbars, and Menus . . . . . . . . . . . . . . . . . . . . . . . . 593 The ItemsControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594 The ListView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602 The TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625 Toolbars and Status Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636

■CHAPTER 19 Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 Understanding Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 Flow Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 Read-Only Flow Document Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 Editing a Flow Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 Fixed Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688

■CHAPTER 20 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 Basic Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 Custom Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702 Print Settings and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711 Printing Through XPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720

■CHAPTER 21 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721 Understanding WPF Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721 Basic Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723 Declarative Animation and Storyboards . . . . . . . . . . . . . . . . . . . . . . . . . . . 738 Animation Types Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774

ix

x

■CONTENTS

■CHAPTER 22 Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775 Playing WAV Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775 The MediaPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779 The MediaElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781 Speech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799

■CHAPTER 23 3-D Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801 3-D Drawing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 Deeper into 3-D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815 Interactivity and Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

■CHAPTER 24 Custom Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 Understanding Custom Elements in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . 844 Building a Basic User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846 Lookless Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858 Extending an Existing Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867 Custom Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875 Custom-Drawn Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890

■CHAPTER 25 Interacting with Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . 891 Assessing Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891 Integrating Windows and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894 Creating Windows with Mixed Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913

■CHAPTER 26 ClickOnce Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915 Application Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915 A Simple ClickOnce Publication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919 ClickOnce Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927 The Last Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933

About the Author ■MATTHEW MACDONALD is an author, educator, and Microsoft MVP. 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 2.0 in C# 2005, Special Edition (Apress, 2006) with Mario Szpuszta. He lives in Toronto with his wife and daughter.

xi

About the Technical Reviewer ■CHRISTOPHE NASARRE is a development architect for Business Objects, a company that develops desktop and Web-based business intelligence solutions. During his spare time, Christophe writes articles for MSDN Magazine, MSDN/Vista, and ASPToday, and he has reviewed books on Win32, COM, MFC, .NET, and WPF since 1996.

xii

Acknowledgments N

o author can complete a book without a small army of helpful individuals. I’m deeply indebted to the whole Apress team, including Tracy Brown Collins, who remained unfazed as the book grew ever bigger, Laura Cheu, who troubleshot obscure figure-sizing issues, Jennifer Whipple and Kim Wimpsett, who speedily performed the copy edit, and many other individuals who worked behind the scenes indexing pages, drawing figures, and proofreading the final copy. I also owe a special thanks to Gary Cornell, who always offers invaluable advice about projects and the publishing world. Christophe Nasarre deserves my sincere thanks for his unfailingly excellent and insightful tech review comments—they’ve helped me to fill gaps and improve the overall quality of this book. I’m also thankful for the legions of die-hard bloggers on the various WPF teams, who never fail to shed light on the deepest recesses of WPF. I encourage anyone who wants to learn more about the future of WPF to track them down. Finally, I’d never write any book without the support of my wife and these special individuals: Nora, Razia, Paul, and Hamid. Thanks everyone!

xiii

Introduction W

hen .NET first appeared, it introduced a small avalanche of new technologies. There was a whole new way to write web applications (ASP.NET), a whole new way to connect to databases (ADO.NET), new typesafe languages (C# and VB .NET), and a managed runtime (the CLR). Not least among these new technologies is Windows Forms, a library of classes for building Windows applications. Although Windows Forms is a mature and full-featured toolkit, it’s hardwired to essential bits of Windows plumbing that haven’t changed much in the past ten years. Most significantly, Windows Forms relies on the Windows API to create the visual appearance of standard user interface elements such as buttons, text boxes, check boxes, and so on. As a result, these ingredients are essentially uncustomizable. For example, if you want to create a stylish glow button you need to create a custom control and paint every aspect of the button (in all its different states) using a lower-level drawing model. And don’t even think about introducing animated effects such as spinning text, shimmering buttons, shrinking windows, or live previews because you’ll have to paint every detail by hand! The Windows Presentation Foundation (WPF) changes all this by introducing a new model with entirely different plumbing. Although WPF includes the standard controls you’re familiar with, it draws every text, border, and background fill itself. As a result, WPF can provide much more powerful features that let you alter the way any piece of screen content is rendered. Using these features, you can restyle common controls such as buttons, often without writing any code. Similarly, you can use transformation objects to rotate, stretch, scale, and skew anything in your user interface, and you can even use WPF’s baked-in animation system to do it right before the user’s eyes. Underlying the new features in WPF is a powerful new infrastructure based on DirectX, the hardware-accelerated graphics API that’s commonly used in cutting-edge computer games. This means that you can use rich graphical effects without incurring the performance overhead that you’d suffer with Windows Forms. In fact, you even get advanced features such as support for video files and 3-D content. Using these features (and a good design tool), it’s possible to create eye-popping user interfaces and visual effects that would have been all but impossible with Windows Forms. Although the cutting-edge video, animation, and 3-D features often get the most attention in WPF, it’s important to note that you can use WPF to build an ordinary Windows application with standard controls and a straightforward visual appearance. In fact, it’s just as easy to use common controls in WPF as it is in Windows Forms. Even better, WPF enhances features that appeal directly to business developers, including a vastly improved data binding model, a new set of classes for printing content and managing print queues, and a document feature for displaying large amounts of formatted text. You’ll even get a new model for building page-based applications that run seamlessly in Internet Explorer and can be launched from a website, all without the usual security warnings and irritating installation prompts. xiv

■INTRODUCTION

Overall, WPF combines the best of the old world of Windows development with new innovations for building modern, graphically rich user interfaces. Although Windows Forms applications will continue to live on for years, developers embarking on new Windows development projects should dig into WPF.

About This Book This book is an in-depth exploration of WPF for professional developers who know the .NET platform, the C# language, and the Visual Studio development environment. Previous experience with Windows Forms is useful but not required to get the most out of this book. This book provides a complete description of every major WPF feature, from XAML (the markup language used to define WPF user interfaces) to 3-D drawing and animation. Along the way, you’ll occasionally work with code that involves other features of the .NET Framework, such as the ADO.NET classes you use to query a database. These features aren’t discussed here. Instead, if you want more information about .NET features that aren’t specific to WPF, you can refer to one of the many dedicated .NET titles from Apress.

Chapter Overview This book includes 26 chapters. If you’re just starting out with WPF, you’ll find it’s easiest to read them in order, as later chapters often draw on the techniques demonstrated in earlier chapters. The following list gives you a quick preview of each chapter: Chapter 1: Introducing WPF describes the architecture of WPF, its DirectX plumbing, and the new device-independent measurement system that resizes user interfaces automatically. Chapter 2: XAML describes the XAML standard that you use to define user interfaces. You’ll learn why it was created and how it works, and you’ll create a basic WPF window using different coding approaches. Chapter 3: The Application introduces the WPF application model. You’ll see how to create single-instance and document-based WPF applications, and you’ll learn how to use the WPF threading model. Chapter 4: Layout delves into the layout panels that allow you to organize elements in a WPF window. You’ll consider different layout strategies, and you’ll build some common types of windows. Chapter 5: Content describes the WPF content control model, which allows you to place elements inside other elements to customize the look of common controls such as buttons and labels. Chapter 6: Dependency Properties and Routed Events describes how WPF extends .NET’s property and event system. You’ll see how WPF uses dependency properties to provide support for key features such as data binding and animation, and how it uses event routing to send events bubbling or tunneling through the elements in your user interface.

xv

xvi

■INTRODUCTION

Chapter 7: Classic Controls considers some of the common controls every Windows developer is familiar with, such as buttons, text boxes, and labels—and their WPF twists. Chapter 8: Windows examines how windows work in WPF. You’ll also learn how to create irregularly shaped windows and use Vista glass effects. Chapter 9: Pages and Navigation describes how you can build pages in WPF and keep track of navigation history. You’ll also see how to build a browser-hosted WPF application that can be launched from a website without a tedious installation step. Chapter 10: Commands introduces the WPF command model, which allows you to wire multiple controls to the same logical action. Chapter 11: Resources describes how resources let you embed binary files in your assembly and reuse important objects throughout your user interface. Chapter 12: Styles explains the WPF style system, which lets you apply a set of common property values to an entire group of controls. Chapter 13: Shapes, Transforms, and Brushes introduces the 2-D drawing model in WPF. You’ll learn to create shapes, alter elements with transforms, and paint exotic effects with gradients, tiles, and images. Chapter 14: Geometries, Drawings, and Visuals delves deeper into 2-D drawing. You’ll learn to create complex paths that incorporate arcs and curves, how to use complex graphics efficiently, and how to use the lower-level visual layer for optimized drawing. Chapter 15: Control Templates shows you how you can give any WPF control a dramatic new look (and new behavior) by plugging in a customized template. You’ll also see how templates allow you to build a “skinnable” application. Chapter 16: Data Binding introduces WPF data binding. You’ll see how to bind any type of object to your user interface, whether it’s an instance of a custom data class or the fullfledged ADO.NET DataSet. You’ll also learn how to convert, format, and validate data. Chapter 17: Data Templates, Data Views, and Data Providers shows some of the tricks for designing professional data-driven interfaces. Along the way, you’ll build rich data lists that incorporate pictures, controls, and selection effects. Chapter 18: Lists, Trees, Toolbars, and Menus considers WPF’s family of list controls. You’ll see data-oriented controls such as grids and trees, and command-oriented controls such as toolbars and menus. Chapter 19: Documents introduces WPF’s rich document support. You’ll learn to use flow documents to present large amounts of text in the most readable way possible, and you’ll use fixed documents to show print-ready pages. You’ll even use the RichTextBox to provide document editing. Chapter 20: Printing demonstrates WPF’s new printing model, which lets you “draw” text and shapes in a print document. You’ll also learn how to manage page settings and print queues.

■INTRODUCTION

Chapter 21: Animation explores WPF’s animation framework, which lets you integrate dynamic effects into your application using straightforward, declarative markup. Chapter 22: Sound and Video describes WPF’s media support. You’ll see how to control playback for sound and video, and how to throw in synchronized animations and live effects. Chapter 23: 3-D Drawing explores the support for drawing 3-D shapes in WPF. You’ll learn how to create, transform, and animate 3-D objects. You’ll even see how to place interactive 2-D controls on 3-D surfaces. Chapter 24: Custom Elements explores how you can extend the existing WPF controls and create your own. You’ll see several examples, including a template-based color picker, a masked text box, and a decorator that performs custom drawing. Chapter 25: Interacting with Windows Forms examines how you can combine WPF and Windows Forms content in the same application—and even in the same window. Chapter 26: ClickOnce Deployment shows how you can deploy WPF applications using the ClickOnce setup model introduced in .NET 2.0.

What You Need to Use This Book In order to run WPF applications, your computer must be running Microsoft Windows Vista or Microsoft Windows XP with Service Pack 2. In order to create WPF applications (and open the sample projects included with this book), you need to use one of the following versions of Visual Studio: • Visual Studio 2005 with .NET 3.0 and the WPF extensions • The next version of Visual Studio (code-named “Orcas”) To track down the exact links you need to install .NET 3.0 and the WPF extensions, visit the page of links for this book at http://www.prosetech.com. There’s one other option. Instead of using any version of Visual Studio, you can use Expression Blend—a graphically oriented design tool—to build and test WPF applications. Overall, Expression Blend is intended for graphic designers who spend their time creating serious eye candy, while Visual Studio is ideal for code-heavy application programmers. This book assumes you’re using Visual Studio. If you’d like to learn more about Expression Blend, you can consult one of many dedicated books on the subject, including Pro Expression Blend by Darren Lutz and Ryan Moore (Apress, 2007). Some of the examples in this book use ADO.NET data access code to query a SQL Server database. To try out these examples, you can use the script file that’s included with the sample code to install the database (on SQL Server version 2000 or later). Alternatively, you can use a file-based database component that’s also included with the sample code. This component retrieves the same data from an XML file, simulating the work of the full database component without requiring a live instance of SQL Server.

xvii

xviii

■INTRODUCTION

VISUAL STUDIO ORCAS At the time of this writing, Visual Studio Orcas was available in an early beta form. If you choose to use it, you’ll need to convert the example projects included with this book before you can open them. The first time you open one of the projects, Visual Studio will launch a conversion wizard that leads you through the process. The conversion process doesn’t change the actual code in these examples—instead, it simply updates the .csproj files to use the new format. Visual Studio Orcas offers one obvious advantage over Visual Studio 2005—it includes design-time support that allows you to build WPF user interfaces by pointing and clicking, much as you design Windows Forms applications. With Visual Studio 2005, you’re able to preview your user interfaces at design time, but you need to do much of the coding by hand. Technically, Visual Studio 2005 uses .NET 3.0, which includes WPF 1.0. Visual Studio Orcas includes .NET 3.5, with a slightly newer version of WPF. This newer, unreleased version of WPF has few significant changes. The key differences are bug fixes, performance tune-ups, and support for LINQ (a set of language extensions in .NET 3.5 that provides SQL-like querying capabilities).

Code Samples and URLs It’s a good idea to check the Apress website or http://www.prosetech.com to download the most recent up-to-date code samples. You’ll need to do this to test most of the more sophisticated code examples described in this book because the less significant details are usually left out. This book focuses on the most important sections so that you don’t need to wade through needless extra pages to understand a concept. To download the source code, surf to http://www.prosetech.com and look for the page for this book. You’ll also find a list of links that are mentioned in this book, so you can find important tools and examples without needless typing.

Feedback This book has the ambitious goal of being the best tutorial and reference for programming WPF. Toward that end, your comments and suggestions are extremely helpful. You can send complaints, adulation, and everything in between directly to [email protected]. I can’t solve your .NET problems or critique your code, but I will benefit from information about what this book did right and wrong (or what it may have done in an utterly confusing way).