WPF in Action with Visual Studio 2008
Total Page:16
File Type:pdf, Size:1020Kb
www.it-ebooks.info WPF in Action with Visual Studio 2008 www.it-ebooks.info www.it-ebooks.info 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.) www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info contents preface xvii acknowledgments xix about this book xxi about the cover illustration xxv PART 1 PAST, PRESENT, AND FUTURE ...............................1 The road to Avalon (WPF) 3 1 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 www.it-ebooks.info viii CONTENTS Getting started with WPF and Visual Studio 2008 22 2 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 WPF from 723 feet 41 3 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 Working with layouts 65 4 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 www.it-ebooks.info 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 The Grid panel 94 5 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 Resources, styles, control templates, and themes 119 6 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 Events 147 7 7.1 Routed events 148 Bubbling events 149 ■ Tunneling events 151 www.it-ebooks.info x CONTENTS 7.2 Events from code 154 handledEventsToo 155 ■ Class events 156 7.3 Summary 156 Oooh, shiny! 157 8 8.1 Glass buttons 158 Styling the text 162 ■ Adding glow when over buttons 162 Handling the button click 164 8.2 Adding some simple animation 165 Animating button glow 165 ■ Animating a color 168 8.3 Reflections 169 8.4 Transforms 173 8.5 Summary 174 PART 3 APPLICATION DEVELOPMENT ...........................177 Laying out a more complex application 179 9 9.1 Creating the Desktop Wiki Project 181 9.2 Nesting layouts 182 Preparing the layout for menus and toolbars 183 Adding menubars, statusbars, and toolbars… 184 9.3 Nested layouts 186 Adding the first Grid 187 ■ Adding the second Grid 188 Using a StackPanel and Expander as navigation aids 189 9.4 Summary 190 Commands 191 10 10.1 A brief history of commands 192 Windows Forms and simple event handlers 192 Son of MFC 193 10.2 The WPF approach 194 The Command pattern 194 ■ WPF commands 195 10.3 Using the built-in system commands 196 ApplicationCommands 197 ■ NavigationCommands 198 EditingCommands 198 ■ Component and media commands 200 www.it-ebooks.info CONTENTS xi 10.4 Handling commands 200 Handling a built-in command 200 ■ Creating a custom command 201 ■ Shortcuts and gestures 202 10.5 Command routing 203 10.6 A cleaner custom command implementation 204 Implementing a RoutedUICommand 204 ■ Adding a CommandBinding 206 10.7 Summary 208 Data binding with WPF 209 11 11.1 WPF data binding 210 11.2 ProcessMonitor: A simple binding example 212 Binding Data with XAML 212 ■ Binding in code 217 Binding notation and options 219 11.3 Binding to XML 223 Creating the CVE Viewer application 225 ■ Binding controls to XML 227 ■ XPath binding notation 228 ■ Path versus XPath 229 ■ Understanding and using DataContexts 230 Master-Detail Binding 233 11.4 Binding to ADO.NET database objects 234 Creating a bookmark utility 236 ■ Creating the simple DAL 236 Laying out the UI and creating data bindings 238 11.5 Binding to business objects 242 Creating a WikiPage business object 242 ObservableCollection 243 ■ Create a model façade 244 Wiring business objects to presentation objects 246 11.6 Binding to LINQ data 250 11.7 Summary 252 Advanced data templates and binding 253 12 12.1 Data converters 254 Formatting bound data with StringFormat 255 ■ A number to formatted string data converter 259 ■ Converter parameters 261 12.2 DataTriggers 263 12.3 CollectionViewSource 266 Sorting with CollectionViewSource 266 ■ Programatically sorting with CollectionViewSource 268 ■ Filtering with CollectionViewSource 270 www.it-ebooks.info xii CONTENTS 12.4 Conditional templates 273 A more involved template 273 ■ Conditionally using a template 275 ■ Templates based on type 277 12.5 Validators 278 The ExceptionValidationRule 278 ■ Custom ErrorTemplates 280 ■ Custom validation rules 282 12.6 Model-View-ViewModel 283 12.7 Advanced binding capabilities 285 Hierarchical binding 285 ■ MultiBinding 289 PriorityBinding 295 12.8 Summary 298 Custom controls 299 13 13.1 Composing new user controls 301 Building a LinkLabel control 301 Testing the LinkLabel UserControl 304 13.2 Building custom controls 306 Building a control library 307 ■ Create the new custom control 308 ■ Create the default template for the control 310 Testing the control 312 ■ Customizing a custom control with a template 313 13.3 Summary 314 Drawing 315 14 14.1 Drawing with Shapes 317 Shapes in XAML 317 ■ Stupid