O'reilly Programming WPF (2Nd Edition).Pdf

O'reilly Programming WPF (2Nd Edition).Pdf

SECOND EDITION Programming WPF Chris Sells and Ian Griffiths Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Programming WPF, Second Edition by Chris Sells and Ian Griffiths Copyright © 2007, 2005 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: John Osborn Indexer: John Bickelhaupt Production Editor: Rachel Monaghan Cover Designer: Karen Montgomery Copyeditor: Audrey Doyle Interior Designer: David Futato Proofreader: Rachel Monaghan Illustrators: Robert Romano and Jessamyn Read Printing History: August 2007: Second Edition. September 2005: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming WPF, the image of a kudu, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN-10: 0-596-51037-3 ISBN-13: 978-0-596-51037-4 [C] Abi: Thank you for everything. My parents: Thank you for making it all possible. —Ian Griffiths My wife and my sons: You define the heaven that exceeds my grasp. Both my parents: You made me love reading from the beginning. I was happy that you passed on the secret writer gene (not to mention surprised). —Chris Sells Table of Contents Forewords . xi Preface . xv 1. Hello, WPF . 1 WPF from Scratch 1 XAML Browser Applications (XBAPs) 14 Content Models 16 Layout 19 Controls 22 Data Binding 22 Dependency Properties 27 Resources 28 Styles 30 Animation 31 Control Templates 32 Graphics 33 3D 34 Documents and Printing 34 2. Applications and Settings . 36 Application Lifetime 36 Application Deployment 48 Settings 55 v 3. Layout . 61 Layout Basics 61 StackPanel 62 WrapPanel 65 DockPanel 66 Grid 69 Canvas 84 Viewbox 86 Common Layout Properties 89 When Content Doesn’t Fit 99 ScrollViewer 101 Custom Layout 105 4. Input . 109 Routed Events 109 Mouse Input 117 Keyboard Input 120 Ink Input 122 Commands 124 Code-Based Input Handling Versus Triggers 137 5. Controls . 139 What Are Controls? 139 Buttons 141 Slider and Scroll Controls 144 ProgressBar 145 Text Controls 146 ToolTip 149 GroupBox and Expander 150 List Controls 152 Menus 160 Toolbars 164 GridSplitter 166 6. Simple Data Binding . 168 Without Data Binding 168 Data Binding 177 Debugging Data Binding 198 vi | Table of Contents 7. Binding to List Data . 200 Binding to List Data 200 Data Source Providers 228 Master-Detail Binding 245 Hierarchical Binding 252 8. Styles . 257 Without Styles 257 Inline Styles 261 Named Styles 262 Element-Typed Styles 268 Data Templates and Styles 271 Triggers 275 9. Control Templates . 284 Beyond Styles 284 Logical and Visual Trees 305 Data-Driven UI 308 10. Windows and Dialogs . 314 Window 314 Dialogs 322 11. Navigation . 341 NavigationWindow 341 Pages 342 Frames 359 XBAPs 361 Navigation to HTML 363 12. Resources . 365 Creating and Using Resources 365 Resources and Styles 378 Binary Resources 383 Global Applications 389 13. Graphics . 395 Graphics Fundamentals 395 Shapes 406 Bitmaps 429 Table of Contents | vii Brushes and Pens 439 Transformations 461 Visual Layer Programming 463 14. Text and Flow Documents . 468 Fonts and Text Styles 468 Text and the User Interface 478 Text Object Model 493 Typography 519 15. Printing and XPS . 522 XPS 522 XPS Document Classes 524 Generating XPS Output 533 XPS File Generation Features 543 System.Printing 555 Displaying Fixed Documents 561 16. Animation and Media . 563 Animation Fundamentals 563 Timelines 579 Keyframe Animations 593 Path Animations 598 Clocks and Control 601 Transition Animations 605 Audio and Video 608 17. 3D Graphics . 612 3D Content in a 2D World 612 Cameras 613 Models 618 Lights 629 Textures 635 Transforms 637 3D Data Visualization 642 Hit Testing 648 viii | Table of Contents 18. Custom Controls . 651 Custom Control Basics 651 Choosing a Base Class 652 Custom Functionality 655 Supporting Templates in Custom Controls 668 Default Styles 674 UserControl 676 Adorners 678 A. XAML . 683 B. Interoperability . 715 C. Asynchronous and Multithreaded WPF Programming . 738 D. WPF Base Types . 750 E. Silverlight . 766 Index . 821 Table of Contents | ix Forewords 1 First Edition Over the past two-plus years, my day job has involved XAML-izing various parts of the Microsoft universe. My standard refrain when encountering XAML newbies has been “read the XAML appendix from Chris and Ian’s book.” That appendix (origi- nally printed in the beta edition of this book) was easily the most direct and to-the- point treatment of the topicI’ve seen, and several dozen of my coworkersgot their first taste of XAML from Ian’s excellent writing. (Ian wrote the XAML appendix.) Over the past year, as I’ve started to make the transition from runtime plumber to pixel pusher, the chapters on WPF proper were super-efficient in getting me off the ground (things have changed a lot since I wrote my last WndProc). At the time this edition hits the shelves, there are numerous books dedicated to WPF, written by some pretty notable folks. This book is unique in that Ian has been telling the story on the road for a couple of years getting the right balance of concep- tual understanding and pragmatic “everyone screws this up” experience. I know from personal experience that there’s nothing like teaching to hone a story to perfec- tion—this book is evidence of that. Ian’s co-author should thank his lucky stars that Ian was willing to travel the planet trying out the material rather than taking a cushy job in Windows. Now that they’ve gotten this book out, maybe Ian should take a cushy job, too. He’s certainly earned it. Second Edition Wow, I can’t believe that after all that time in the chute, .NET 3.0 and Windows Vista have finally shipped. I vividly remember scrambling backstage at PDC 2003 with Chris trying to ready the first live demonstration of .NET 3.0 (then called WinFX) for the keynote speaker, xi Jim Allchin. It was an especially stressful keynote because Los Angeles was plagued with brush fires at the time and Chris Anderson’s flight had been canceled; fortu- nately Chris Sells had already arrived and was ready to pinch-hit both in preparation and presentation if Chris, in fact, couldn’t make it to L.A. in time. At the time, Chris’ job at Microsoft was to make sure that Vista—including WPF—was a smashing suc- cess. Little did he know it would take almost four years until the product actually shipped (which of course is a prerequisite for success). So, what’s the big deal with WPF? Like its sister .NET 3.0 technology, Windows Workflow Foundation (WF), WPF embraces the “it takes a village” approach to software development and uses XAML to allow people with different skill sets to collaborate in the development process. In the case of WF, XAML lets high-level process and rule descriptions integrate with imperative code written in C# or Visual Basic. In the case of WPF, XAML is the bridge between us code monkeys and the beret-wearing, black-turtleneck set who design visuals that look like they weren’t designed by, well, us code monkeys. WPF really is an impressive piece of technology: documents, forms, and multimedia all wrapped up nicely in a markup- and code-friendly package. What I find even more impressive is the fact that Chris found the time outside his day job to pull together the book you’re holding in your hands right now, capturing those four-plus years of experience with WPF (including screenshots!) into a digest- ible and portable form. I’ve had the good fortune of having many conversations with Chris over the years about the nuances of WPF—sometimes on the phone, sometimes in his office (it’s across the hall from mine), and sometimes at the poker table. This book has taught me a whole lot more. Now that it’s all shipped, let the light blinking begin! —Don Box Architect, Microsoft When I joined Microsoft 11 years ago, I first worked in the IT group, building applica- tions to help the Microsoft sales force analyze data. I developed using Visual Basic 4.0 on early versions of Windows 95 and Windows NT 3.51 before moving over to work on the development team for Visual Basic5.0, and later, 6.0. As time went on, I worked on Visual J++, Windows Foundation Classes, .NET, Windows Forms, ASP.NET, and eventually the Windows Presentation Foundation (WPF). xii | Forewords When I learned to program Windows, I read the book that was considered the “bible” of Windows programming at the time, Programming Windows 3.1 by Charles Petzold (Microsoft Press). After helping to build the next-generation programming platform for Microsoft—the .NET Framework—I was first introduced to Chris Sells because he’d written the “bible” of programming .NET client applications: Windows Forms Programming (Addison-Wesley).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    867 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us