www.it-ebooks.info UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA Adam Nathan Windows ® Presentation Foundation with Daniel Lehenbauer, Lead Developer Responsible for WPF 3D www.it-ebooks.info Windows Presentation Foundation Unleashed Copyright © 2007 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omis- sions. Nor is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-32891-7 Library of Congress Cataloging-in-Publication Data Nathan, Adam. Windows presentation foundation unleashed / Adam Nathan. p. cm. ISBN 0-672-32891-7 1. Windows presentation foundation. 2. Application software. 3. Microsoft .NET Framework. I. Title. QA76.76.A65N38 2007 005.2’768—dc22 2006038586 Printed in the United States of America First Printing: December, 2006 04030201 4321 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Bulk Sales Pearson Education offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearsoned.com Editor-in-Chief Karen Gettman Acquisitions Editor Neil Rowe Development Editor Mark Renfrow Managing Editor Gina Kanouse Project Editor Betsy Harris Copy Editor Karen Annett Indexers Lisa Stumpf Ken Johnson Proofreader Kathy Bidnell Publishing Coordinator Cindy Teeters Book Designer Gary Adair Technical Editor Robert Hogue Composition Jake McFarland www.it-ebooks.info Contents at a Glance Introduction 1 PART I Background 1 Why Windows Presentation Foundation? 9 2 XAML Demystified 19 3 Important New Concepts in WPF 45 Part II Building a WPF Application 4 Introducing WPF’s Controls 85 5 Sizing, Positioning, and Transforming Elements 127 6 Layout with Panels 147 7 Structuring and Deploying an Application 191 Part III Features for Professional Developers 8 Resources 239 9 Data Binding 259 10 Styles, Templates, Skins, and Themes 307 Part IV Going Beyond Today’s Applications with Rich Media 11 2D Graphics 355 12 3D Graphics 415 13 Animation 469 14 Audio, Video, Speech, and Documents 501 Part V Advanced Topics 15 Interoperability with Win32, Windows Forms, and ActiveX 539 16 User Controls and Custom Controls 579 17 Layout with Custom Panels 605 Part VI Appendix Appendix Helpful Tools 617 Index 621 www.it-ebooks.info Table of Contents Introduction 1 Who Should Read This Book? 2 Software Requirements 3 Code Examples 4 How This Book Is Organized 4 Part I: Background 4 Part II: Building a WPF Application 4 Part III: Features for Professional Developers 5 Part IV: Going Beyond Today’s Applications with Rich Media 5 Part V: Advanced Topics 5 Conventions Used in This Book 6 PART I Background 1 Why Windows Presentation Foundation? 9 A Look at the Past 10 Enter WPF 11 Part of the .NET Framework 16 Designed for Managed Code 16 Emphasis on Declarative Descriptions 17 Conclusion 18 2 XAML Demystified 19 XAML Defined 20 Elements and Attributes 20 Namespaces 22 Property Elements 24 Type Converters 25 Markup Extensions 26 Children of Object Elements 29 The Content Property 29 Collection Items 30 More Type Conversion 32 Compilation: Mixing XAML with Procedural Code 34 Loading and Parsing XAML at Run-Time 34 Compiling XAML 36 XAML Keywords 41 Windows Presentation Foundation Unleashed iv www.it-ebooks.info Conclusion 43 Complaint #1: XML Is Too Verbose to Type 44 Complaint #2: XML-Based Systems Have Poor Performance 44 3 Important New Concepts in WPF 45 Logical and Visual Trees 45 Dependency Properties 51 A Dependency Property Implementation 51 Change Notification 53 Property Value Inheritance 55 Support for Multiple Providers 57 Attached Properties 60 Routed Events 64 A Routed Event Implementation 64 Routing Strategies and Event Handlers 66 Routed Events in Action 66 Attached Events 70 Commands 73 Built-In Commands 74 Executing Commands with Input Gestures 77 Controls with Built-In Command Bindings 78 A Tour of the Class Hierarchy 79 Conclusion 81 Part II Building a WPF Application 4 Introducing WPF’s Controls 85 Content Controls 86 Buttons 87 Simple Containers 91 Containers with a Header 95 Items Controls 97 Selectors 100 Menus 111 Other Items Controls 115 Range Controls 120 ProgressBar 120 Slider 121 Text and Ink Controls 122 TextBox 122 RichTextBox 123 PasswordBox 123 InkCanvas 124 Conclusion 126 Contents v www.it-ebooks.info 5 Sizing, Positioning, and Transforming Elements 127 Controlling Size 128 Height and Width 128 Margin and Padding 130 Visibility 132 Controlling Position 133 Alignment 133 Content Alignment 134 FlowDirection 135 Applying Transforms 136 RotateTransform 138 ScaleTransform 139 SkewTransform 142 TranslateTransform 142 MatrixTransform 142 Combining Transforms 143 Conclusion 145 6 Layout with Panels 147 Canvas 148 StackPanel 151 WrapPanel 152 DockPanel 155 Grid 157 Sizing the Rows and Columns 161 Interactive Sizing with GridSplitter 164 Sharing Row and Column Sizes 166 Comparing Grid to Other Panels 168 Primitive Panels 169 TabPanel 169 ToolBarOverflowPanel 170 ToolBarTray 170 UniformGrid 170 Handling Content Overflow 170 Clipping 171 Scrolling 173 Scaling 175 Putting It All Together: Creating a Visual Studio-Like Collapsible, Dockable, Resizable Pane 179 Conclusion 189 Windows Presentation Foundation Unleashed vi www.it-ebooks.info 7 Structuring and Deploying an Application 191 Standard Windows Applications 191 The Window Class 192 The Application Class 195 Creating and Showing Dialogs 201 Persisting and Restoring Application State 204 Deployment: ClickOnce Versus Windows Installer 205 Navigation-Based Windows Applications 206 Pages and Their Navigation Containers 207 Navigating from Page to Page 209 Passing Data Between Pages 215 Applications with a Windows Vista Look and Feel 218 Going Beyond MessageBox with TaskDialog 219 Using Aero Glass 221 Gadget-Style Applications 225 XAML Browser Applications 227 Limited Feature Set 229 Integrated Navigation 231 Deployment 232 Loose XAML Pages 235 Conclusion 236 Part III Features for Professional Developers 8 Resources 239 Binary Resources 239 Defining Binary Resources 240 Accessing Binary Resources 241 Localization 246 Logical Resources 247 Resource Lookup 250 Static Versus Dynamic Resources 251 Interaction with System Resources 256 Conclusion 256 9 Data Binding 259 Introducing the Binding Object 259 Using Binding in Procedural Code 259 Using Binding in XAML 262 Binding to Plain .NET Properties 263 Binding to an Entire Object 265 Binding to a Collection 267 Sharing the Source with DataContext 270 Contents vii www.it-ebooks.info Controlling Rendering 271 Using Data Templates 271 Using Value Converters 274 Customizing the View of a Collection 279 Sorting 279 Grouping 281 Filtering 284 Navigating 285 Working with Additional Views 286 Data Providers 288 XmlDataProvider 289 ObjectDataProvider 293 Advanced Topics 296 Customizing the Data Flow 296 Adding Validation Rules to Binding 298 Working with Disjoint Sources 301 Putting It All Together: The Pure-XAML RSS Reader 303 Conclusion 305 10 Styles, Templates, Skins, and Themes 307 Styles 308 Sharing Styles 310 Triggers 316 Templates 321 Introducing Control Templates 321 Getting Interactivity with Triggers 322 Restricting the Target Type 324 Respecting the Templated Parent’s Properties 325 Respecting Visual States 332 Mixing Templates with Styles 338 Skins 340 Themes 346 Using System Colors, Fonts, and Parameters 347 Per-Theme Styles and Templates 348 Conclusion 351 Part IV Going Beyond Today’s Applications with Rich Media 11 2D Graphics 355 Drawings 356 Geometries 358 Pens 369 Clip Art Example 370 Windows Presentation Foundation Unleashed viii www.it-ebooks.info Visuals 372 Filling a DrawingVisual with Content 373 Displaying a Visual on the Screen 376 Visual Hit Testing 378 Shapes 385 Rectangle 386 Ellipse 387 Line 388 Polyline 388 Polygon 389 Path 390 Clip Art Based on Shapes 391 Brushes 392 Color Brushes 392 Tile Brushes 399 Brushes as Opacity Masks 406 Bitmap Effects 409 Conclusion 413 12 3D Graphics 415 Getting Started with 3D Graphics 416 Cameras and Coordinate Systems 420 The Position Property 420 LookDirection 422 UpDirection 425 Orthographic Versus Perspective 428 Transform3Ds 430 TranslateTransform3Ds 433 ScaleTransform3Ds 433 RotateTransform3Ds 436 Combining Transform3Ds 439 Model3Ds 439 Lights 440 GeometryModel3Ds 447 Model3DGroup 459 Visual3Ds 462 ModelVisual3Ds 462 3D Hit Testing 464 The Viewport3D Element 465 Conclusion 468 Contents ix www.it-ebooks.info [...]... released simultaneously with Windows Vista and is installed with the operating system by default, it is also supported on Windows XP (including Media Center, Tablet PC, and x64 editions) and Windows Server 2003 .NET Framework 3.0 Windows Communication Foundation (WCF) Windows Presentation Foundation (WPF) Windows Workflow Foundation (WF) Windows CardSpace (WCS) NET Framework 2.0 Windows Forms ASP.NET ADO.NET... picking up Windows Presentation Foundation Unleashed! To avoid unsatisfied customers, I want to clarify that this is not a book about Microsoft PowerPoint (which many people consider to be the foundation of Windows presentations)! Windows Presentation Foundation (WPF) is Microsoft’s latest technology for creating graphical user interfaces, whether they consist of plain forms, document-centric windows, ... CHAPTER 1 Why Windows Presentation Foundation? Microsoft now has a solution for helping people create 21st-century software that meets these high demands, using less time and less money This solution is Windows Presentation Foundation (WPF) A Look at the Past The primary technologies behind most of today’s Windows- based user interfaces—the GDI and USER subsystems—were introduced with Windows 1.0 in... final release of version 3.0 of Windows Presentation Foundation, the corresponding Windows SDK, and the October 2006 release of NET Framework 3.0 extensions to Visual Studio 2005 The following software is required: A version of Windows that supports the NET Framework 3.0 This can be Windows XP with Service Pack 2 (including Media Center, Tablet PC, and x64 editions), Windows Server 2003 with Service... there any differences with WPF on Windows Vista versus earlier versions of Windows? WPF doesn’t expose any Windows Vista–specific APIs But two interesting implicit features were unable to be supported on earlier versions of Windows: 3D objects only get anti-aliasing on Windows Vista or later Non-rectangular or translucent windows only get hardware acceleration on Windows Vista or later And of course,... then tell you how to avoid it www.it-ebooks.info PART I Background IN THIS PART CHAPTER 1 Why Windows Presentation Foundation? CHAPTER 2 XAML Demystified 19 CHAPTER 3 Important New Concepts in WPF 45 www.it-ebooks.info 9 This page intentionally left blank www.it-ebooks.info CHAPTER 1 Why Windows Presentation Foundation? In movies and on TV, the main characters are typically an exaggeration of the people... advanced layout) And Chapter 15 shows how you can still use any Windows Forms controls in a WPF application So unless running on Windows 98 is important (which is still supported by Windows Forms 2.0 but not by WPF), I would recommend WPF over Windows Forms for a broad range of applications—especially after Visual Studio “Orcas” is released But Windows Forms isn’t going away anytime soon; there just won’t... language support, and full access to the underlying platform (when security permits) But viewing such content requires Windows and the NET Framework 3.0 (installed by default on Windows Vista or later) To address cross-platform support and ubiquity, Microsoft has announced Windows Presentation Foundation Everywhere (WPF/E) WPF/E (not yet released at the time of writing) is a small, lightweight runtime representing... GDI+ and USER yet provided the kind of productivity that people enjoy with frameworks like Windows Forms Windows Presentation Foundation (WPF) is the answer for software developers and graphic designers who want to create modern user experiences without having to master several difficult technologies Although Presentation sounds like a lofty term for what I would simply call a user interface, it’s... Desktop, don’t handle high dots-per-inch (DPI) settings very well, and have other visual glitches 12 CHAPTER 1 Why Windows Presentation Foundation? Windows Vista Magnifier Non-WPF Content WPF Content Magnified Area FIGURE 1.1 Vector-based WPF content scales beautifully even under the Windows Vista Magnifier program Hardware acceleration—Although WPF is a new technology, it is built on top of Direct3D . Cataloging-in-Publication Data Nathan, Adam. Windows presentation foundation unleashed / Adam Nathan. p. cm. ISBN 0-672-32891-7 1. Windows presentation foundation. 2. Application software. 3. Microsoft. of Windows presentations)! Windows Presentation Foundation (WPF) is Microsoft’s latest technology for creating graphical user interfaces, whether they consist of plain forms, document-centric windows, . 556 Introducing HwndSource 556 Getting the Right Layout 559 Windows Presentation Foundation Unleashed x www.it-ebooks.info Embedding Windows Forms Controls in WPF Applications 563 Embedding a