WPF in Action with Visual Studio 2008 WPF in Action with Visual Studio 2008 COVERS VISUAL STUDIO 2008 SP1 AND .NET 3.5 SP1 ARLEN FELDMAN MAXX DAYMON MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B Fax: (609) 877-8256 Greenwich, CT 06830 Email: [email protected] ©2009 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed elemental chlorine-free Development Editor: Jeff Bleiel Manning Publications Co. Copyeditor: Andrea Kaucher Sound View Court 3B Typesetter: Dennis Dalinnik Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-933988-22-3 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 12 11 10 09 08 brief contents PART 1 PAST, PRESENT, AND FUTURE. .......................................1 1 ■ The road to Avalon (WPF) 3 2 ■ Getting started with WPF and Visual Studio 2008 22 3 ■ WPF from 723 feet 41 PART 2 THE BASICS.................................................................63 4 ■ Working with layouts 65 5 ■ The Grid panel 94 6 ■ Resources, styles, control templates, and themes 119 7 ■ Events 147 8 ■ Oooh, shiny! 157 PART 3 APPLICATION DEVELOPMENT .....................................177 9 ■ Laying out a more complex application 179 10 ■ Commands 191 11 ■ Data binding with WPF 209 12 ■ Advanced data templates and binding 253 v vi BRIEF CONTENTS 13 ■ Custom controls 299 14 ■ Drawing 315 15 ■ Drawing in 3D 352 PART 4 THE LAST MILE.......................................................... 371 16 ■ Building a navigation application 373 17 ■ WPF and browsers: XBAP, ClickOnce, and Silverlight 390 18 ■ Printing, documents, and XPS 406 19 ■ Transition effects 427 20 ■ Interoperability 457 21 ■ Threading 474 contents preface xvii acknowledgments xix about this book xxi about the cover illustration xxv PART 1 PAST, PRESENT, AND FUTURE...............................1 1 The road to Avalon (WPF) 3 1.1 The past and the present 4 Why Windows drawing is the way it is 5 ■ How we currently create Windows UIs 7 ■ Why the web is the way it is 9 ■ How UI is created on the web 10 1.2 Why Avalon/WPF 11 Taking advantage of modern hardware 12 ■ Using modern software design 13 ■ Separating presentation logic from presentation 14 ■ Making it simpler to code GUIs 15 1.3 Creating UI using WPF 16 Defining WPF UI with XAML 16 ■ Defining WPF UI through code 17 ■ Defining WPF UI with tools 18 ■ Who does the drawing 19 ■ Pixels versus vectors 19 1.4 Summary 20 vii viii CONTENTS 2 Getting started with WPF and Visual Studio 2008 22 2.1 Your grandpa’s Hello, World! 23 Adding a button and button-handler to the window 25 Running Hello, World! 27 ■ The TextBlock control 27 2.2 The application definition 30 Defining application startup in XAML 30 Why define the application in XAML? 31 2.3 A tour of WPF in Visual Studio 2008 34 The XAML designer 35 ■ The Properties grid 38 Selection controls in Visual Studio 39 ■ The Document Outline 39 2.4 Summary 40 3 WPF from 723 feet 41 3.1 Where does WPF fit in Windows? 42 Red bits and green bits 42 ■ Silverlight 43 3.2 Framework services 44 Base services 44 ■ Media services 51 ■ User interface services 55 ■ Document services 56 3.3 Necessary and useful tools 58 Microsoft Expression family 59 ■ Visual Studio 60 Other tools 60 3.4 Summary 61 PART 2 THE BASICS.......................................................63 4 Working with layouts 65 4.1 The idea behind layout panels 66 4.2 The Canvas layout 68 Converting a Grid layout to a Canvas layout by modifying the XAML 69 ■ Adding a Canvas to an existing layout 69 Using attached properties 72 ■ Setting up a Canvas programmatically 73 4.3 The StackPanel layout 76 Adding scrolling support 80 ■ The Expander control 81 CONTENTS ix 4.4 The DockPanel layout 83 Defining a DockPanel in XAML 84 ■ Setting up a DockPanel programmatically 85 4.5 The WrapPanel layout 86 4.6 Other layout options 88 Specialized layout panels 89 ■ The FlowDocument 89 4.7 Summary 93 5 The Grid panel 94 5.1 Getting started with the Grid layout panel 95 Modifying the Grid 96 ■ Grid specific properties 100 5.2 Using the Grid layout to build a calculator UI 101 Planning the calculator 101 ■ Laying out the calculator 102 Tweaking appearance 104 5.3 The Grid and localization 107 5.4 UniformGrid 109 5.5 Making the calculator work 110 Handling operations 110 ■ Genericizing the handlers 114 5.6 Summary 117 6 Resources, styles, control templates, and themes 119 6.1 Resources 120 Using standalone resource dictionaries 122 ■ Using resources from code 124 ■ Dynamic resources 125 6.2 Styles 131 Styles based on other styles 133 ■ Implicitly applying styles 135 6.3 Control templates 136 Creating a control template 137 ■ ContentPresenters 137 Template binding 138 ■ Triggers 139 6.4 Themes 140 Using a specific theme 142 ■ Changing themes from code 145 6.5 Summary 146 7 Events 147 7.1 Routed events 148 Bubbling events 149 ■ Tunneling events 151