ebook img

Windows Presentation Foundation Unleashed PDF

655 Pages·2007·17.955 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Windows Presentation Foundation Unleashed

Adam Nathan with Daniel Lehenbauer, Lead Developer Responsible for WPF 3D Windows ® Presentation Foundation U N L E A S H E D 800 East 96th Street,Indianapolis,Indiana 46240 USA Windows Presentation Foundation Unleashed Editor-in-Chief Copyright ©2007 by Sams Publishing Karen Gettman All rights reserved. No part of this book shall be reproduced,stored in a retrieval system,or Acquisitions Editor transmitted by any means,electronic,mechanical,photocopying,recording,or otherwise, without written permission from the publisher. No patent liability is assumed with respect to Neil Rowe 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- Development Editor sions. Nor is any liability assumed for damages resulting from the use of the information Mark Renfrow contained herein. Managing Editor International Standard Book Number: 0-672-32891-7 Gina Kanouse Library of Congress Cataloging-in-Publication Data Nathan,Adam. Project Editor Windows presentation foundation unleashed / Adam Nathan. Betsy Harris p. cm. Copy Editor ISBN 0-672-32891-7 Karen Annett 1. Windows presentation foundation. 2. Application software. 3. Microsoft .NET Framework. I. Title. Indexers Lisa Stumpf QA76.76.A65N38 2007 Ken Johnson 005.2’768—dc22 Proofreader 2006038586 Kathy Bidnell Printed in the United States of America Publishing First Printing: December,2006 Coordinator 04 03 02 01 4 3 2 1 Cindy Teeters Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been Book Designer appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Gary Adair Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Technical Editor Robert Hogue Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible,but Composition no warranty or fitness is implied. The information provided is on an “as is” basis. The author Jake McFarland 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 [email protected] For sales outside of the U.S.,please contact International Sales [email protected] 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 iv Windows Presentation Foundation Unleashed 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 Contents v 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 vi Windows Presentation Foundation Unleashed 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 Contents vii 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 viii Windows Presentation Foundation Unleashed 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 Contents ix 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

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.