Pro WPF in VB 2010 Windows Presentation Foundation in .NET 4 ■ ■ ■ Matthew MacDonald Pro WPF in VB 2010: Windows Presentation Foundation in .NET 4 Copyright © 2010 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-7240-3 ISBN-13 (electronic): 978-1-4302-7241-0 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. Publisher and President: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Fabio Claudio Ferracchiati Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editors: Marilyn Smith and Kim Wimpsett Compositor: Mary Sudul Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko 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 e-mail [email protected], or visit http://www.apress.com. 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 http://www.apress.com/info/bulksales. 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. For my wonderful family, Faria, Maya, and Brenna Contents at a Glance Contents.....................................................................................................................vi About the Author...................................................................................................xxxi About the Technical Reviewer..............................................................................xxxii Acknowledgments...............................................................................................xxxiii Introduction.........................................................................................................xxxiv ■ Chapter 1: Introducing WPF...................................................................................1 ■ Chapter 2: XAML..................................................................................................23 ■ Chapter 3: Layout.................................................................................................61 ■ Chapter 4: Dependency Properties....................................................................105 ■ Chapter 5: Routed Events...................................................................................119 ■ Chapter 6: Controls............................................................................................159 ■ Chapter 7: The Application.................................................................................213 ■ Chapter 8: Element Binding...............................................................................247 ■ Chapter 9: Commands........................................................................................263 ■ Chapter 10: Resources.......................................................................................291 ■ Chapter 11: Styles and Behaviors......................................................................307 ■ Chapter 12: Shapes, Brushes, and Transforms..................................................331 ■ Chapter 13: Geometries and Drawings..............................................................373 ■ Chapter 14: Effects and Visuals.........................................................................397 ■ Chapter 15: Animation Basics............................................................................421 iv ■ CONTENTS AT A GLANCE ■ Chapter 16: Advanced Animation ......................................................................465 ■ Chapter 17: Control Templates..........................................................................497 ■ Chapter 18: Custom Elements............................................................................539 ■ Chapter 19: Data Binding...................................................................................591 ■ Chapter 20: Formatting Bound Data ..................................................................635 ■ Chapter 21: Data Views......................................................................................685 ■ Chapter 22: Lists, Trees, and Grids....................................................................703 ■ Chapter 23: Windows.........................................................................................745 ■ Chapter 24: Pages and Navigation.....................................................................785 ■ Chapter 25: Menus, Toolbars, and Ribbons.......................................................835 ■ Chapter 26: Sound and Video.............................................................................861 ■ Chapter 27: 3-D Drawing...................................................................................885 ■ Chapter 28: Documents......................................................................................929 ■ Chapter 29: Printing...........................................................................................983 ■ Chapter 30: Interacting with Windows Forms.................................................1015 ■ Chapter 31: Multithreading..............................................................................1037 ■ Chapter 32: The Add-in Model.........................................................................1051 ■ Chapter 33: ClickOnce Deployment..................................................................1075 Index.....................................................................................................................1097 v ■ CONTENTS Contents Contents at a Glance..................................................................................................iv About the Author...................................................................................................xxxi About the Technical Reviewer..............................................................................xxxii Acknowledgments...............................................................................................xxxiii Introduction.........................................................................................................xxxiv ■ Chapter 1: Introducing WPF...................................................................................1 The Evolution of Windows Graphics..................................................................................1(cid:2) DirectX: The New Graphics Engine...........................................................................................................2(cid:2) Hardware Acceleration and WPF..............................................................................................................3(cid:2) WPF: A Higher-Level API....................................................................................................4(cid:2) Windows Forms Lives On.........................................................................................................................6(cid:2) DirectX Also Lives On................................................................................................................................6(cid:2) Silverlight.................................................................................................................................................6(cid:2) Resolution Independence..................................................................................................7(cid:2) WPF Units.................................................................................................................................................8(cid:2) System DPI...............................................................................................................................................8(cid:2) Bitmap and Vector Graphics...................................................................................................................11(cid:2) The Architecture of WPF..................................................................................................12(cid:2) The Class Hierarchy................................................................................................................................13(cid:2) WPF 4...............................................................................................................................16(cid:2) New Features.........................................................................................................................................17(cid:2) vi ■ CONTENTS The WPF Toolkit......................................................................................................................................18(cid:2) Visual Studio 2010..................................................................................................................................18(cid:2) The Last Word..................................................................................................................21 ■ Chapter 2: XAML..................................................................................................23 Understanding XAML.......................................................................................................23(cid:2) Graphical User Interfaces Before WPF....................................................................................................24(cid:2) The Variants of XAML.............................................................................................................................25(cid:2) XAML Compilation..................................................................................................................................25(cid:2) XAML Basics....................................................................................................................27(cid:2) XAML Namespaces.................................................................................................................................28(cid:2) The Code-Behind Class..........................................................................................................................29(cid:2) Properties and Events in XAML........................................................................................31(cid:2) Simple Properties and Type Converters.................................................................................................33(cid:2) Complex Properties................................................................................................................................34(cid:2) Markup Extensions.................................................................................................................................36(cid:2) Attached Properties................................................................................................................................37(cid:2) Nesting Elements....................................................................................................................................38(cid:2) Special Characters and Whitespace.......................................................................................................41(cid:2) Events.....................................................................................................................................................42(cid:2) The Full Eight Ball Example....................................................................................................................44(cid:2) Using Types from Other Namespaces..............................................................................45(cid:2) Loading and Compiling XAML..........................................................................................47(cid:2) Code-Only...............................................................................................................................................47(cid:2) Code and Uncompiled XAML...................................................................................................................50(cid:2) Code and Compiled XAML......................................................................................................................52(cid:2) XAML Only..............................................................................................................................................53(cid:2) XAML 2009.......................................................................................................................55(cid:2) Automatic Event Hookup........................................................................................................................55(cid:2) References.............................................................................................................................................56(cid:2) vii ■ CONTENTS Built-in Types.........................................................................................................................................57(cid:2) Advanced Object Creation......................................................................................................................57(cid:2) The Last Word..................................................................................................................59 ■ Chapter 3: Layout.................................................................................................61 Understanding Layout in WPF..........................................................................................61(cid:2) The WPF Layout Philosophy....................................................................................................................62(cid:2) The Layout Process................................................................................................................................63(cid:2) The Layout Containers............................................................................................................................63(cid:2) Simple Layout with the StackPanel.................................................................................65(cid:2) Layout Properties...................................................................................................................................67(cid:2) Alignment...............................................................................................................................................68(cid:2) Margin....................................................................................................................................................69(cid:2) Minimum, Maximum, and Explicit Sizes.................................................................................................71(cid:2) The Border..............................................................................................................................................74(cid:2) The WrapPanel and DockPanel........................................................................................75(cid:2) The WrapPanel.......................................................................................................................................76(cid:2) The DockPanel........................................................................................................................................77(cid:2) Nesting Layout Containers......................................................................................................................79(cid:2) The Grid...........................................................................................................................80(cid:2) Fine-Tuning Rows and Columns.............................................................................................................83(cid:2) Layout Rounding.....................................................................................................................................85(cid:2) Spanning Rows and Columns.................................................................................................................85(cid:2) Split Windows.........................................................................................................................................86(cid:2) Shared Size Groups................................................................................................................................90(cid:2) The UniformGrid......................................................................................................................................93(cid:2) Coordinate-Based Layout with the Canvas......................................................................93(cid:2) Z-Order...................................................................................................................................................95(cid:2) The InkCanvas........................................................................................................................................95(cid:2) viii ■ CONTENTS Layout Examples..............................................................................................................98(cid:2) A Column of Settings..............................................................................................................................98(cid:2) Dynamic Content..................................................................................................................................100(cid:2) A Modular User Interface......................................................................................................................102(cid:2) The Last Word................................................................................................................104 ■ Chapter 4: Dependency Properties....................................................................105 Understanding Dependency Properties.........................................................................105(cid:2) Defining a Dependency Property..........................................................................................................106(cid:2) Registering a Dependency Property.....................................................................................................107(cid:2) Adding a Property Wrapper..................................................................................................................109(cid:2) How WPF Uses Dependency Properties...............................................................................................110(cid:2) Shared Dependency Properties............................................................................................................111(cid:2) Attached Dependency Properties.........................................................................................................112(cid:2) Property Validation........................................................................................................113(cid:2) The Validation Callback........................................................................................................................114(cid:2) The Coercion Callback..........................................................................................................................115(cid:2) The Last Word................................................................................................................117 ■ Chapter 5: Routed Events...................................................................................119 Understanding Routed Events.......................................................................................119(cid:2) Defining, Registering, and Wrapping a Routed Event...........................................................................120(cid:2) Sharing Routed Events.........................................................................................................................121(cid:2) Raising a Routed Event.........................................................................................................................121(cid:2) Handling a Routed Event......................................................................................................................122(cid:2) Event Routing.................................................................................................................124(cid:2) The RoutedEventArgs Class..................................................................................................................125(cid:2) Bubbling Events....................................................................................................................................126(cid:2) Handling a Suppressed Event...............................................................................................................130(cid:2) Attached Events....................................................................................................................................130(cid:2) ix
Description: