1. Trang chủ
  2. » Công Nghệ Thông Tin

wpf control development unleashed

385 482 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 385
Dung lượng 5,7 MB

Nội dung

www.it-ebooks.info ptg WPF Control Development UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA Pavan Podila Kevin Hoffman Building Advanced User Experiences From the Library of Lee Bogdanoff www.it-ebooks.info ptg WPF Control Development Unleashed Copyright © 2010 by Pearson Education, Inc. 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 authors assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. ISBN-13: 978-0-672-33033-9 ISBN-10: 0-672-33033-4 Library of Congress Cataloging-in-Publication Data: Podila, Pavan. WPF control development unleashed : building advanced user experiences / Pavan Podila, Kevin Hoffman. p. cm. ISBN 978-0-672-33033-9 1. Windows presentation foundation. 2. Application software— Development. 3. User interfaces (Computer systems) 4. Microsoft .NET Framework. I. Hoffman, Kevin. II. Title. QA76.76.A65P64 2009 006.7’882—dc22 2009032558 First Printing September 2009 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 possi- ble, but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors 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 or from the use of the DVD or programs accompanying it. Bulk Sales Sams Publishing 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@pearson.com Editor-in-Chief Karen Gettman Executive Editor Neil Rowe Development Editor Mark Renfrow Managing Editor Kristy Hart Project Editor Andy Beaster Copy Editor Geneil Breeze Indexer Brad Herriman Proofreader Water Crest Publishing Publishing Coordinator Cindy Teeters Book Designer Gary Adair Compositor Jake McFarland From the Library of Lee Bogdanoff www.it-ebooks.info ptg Contents at a Glance Part I Thinking in WPF 1 The WPF Design Philosophy 1 2 The Diverse Visual Class Structure 11 3 Getting Started Writing Custom Controls 31 4 Building Custom Panels 49 5 Using Existing Controls 69 6 The Power of Attached Properties 93 Part II Adding Complex Features 7 Advanced Scrolling 113 8 Virtualization 129 9 Creating Advanced Controls and Visual Effects 143 10 Control Skinning and Themes 167 Part III Building Interactivity, 3D, Animations 11 Bridging the 2D and 3D Worlds 185 12 Custom Animations 203 13 Pixel Shader Effects 223 Part IV Bringing the Controls to the Real World 14 Events, Commands, and Focus 243 15 Advanced Data Binding 275 16 Control and Visual Design Tips 295 17 Performance 317 18 Control Automation 331 Index 351 From the Library of Lee Bogdanoff www.it-ebooks.info ptg Table of Contents About the Authors xii Dedications xiii We Want to Hear from You! xv Part I Thinking in WPF 1 The WPF Design Philosophy 1 Data and Behavior 2 Working with Data 3 Templates 3 Presenters 4 Binding and Converters 4 Layout 5 Styles 5 Working with Behaviors 6 The User Experience 8 The User Experience Benevolent Circle 9 A Note on Sample Source Code 9 Summary 10 2 The Diverse Visual Class Structure 11 Introducing the Visual Classes 11 The DispatcherObject Class 12 The DependencyObject Class 12 The Visual and DrawingVisual Classes 13 The FrameworkElement Class 15 The Shape Class 16 The Text Classes 18 The Control Class 19 The ContentControl Class 20 The ContentPresenter Class 20 The ItemsControl Class 21 The UserControl Class 22 From the Library of Lee Bogdanoff www.it-ebooks.info ptg The Panel Class 23 The Decorator Class 24 The Adorner Class 24 The Image Class 25 The Brushes 25 The DataTemplate, ControlTemplate, and ItemsPanelTemplate Classes 27 The Viewport3D Class 27 The MediaElement Class 28 The InkCanvas 28 Summary 29 3 Getting Started Writing Custom Controls 31 Overcoming the “Start from Scratch” Instinct 31 Using Data Transformations 32 Find the Behavior You Want, Then Extend 34 The Power of Attached Properties 35 Custom Control Creation Checklist 38 Thinking in Layers—The Art of Decomposition 39 Sample: Building a Circular Minute Timer 40 Enhancing and Extending the ProgressBar 40 Creating the Arc Shape 42 Working with the ControlTemplate 43 Summary 47 4 Building Custom Panels 49 Layout Defined 49 How Layout Works 51 Working with Visual Children 52 Creating a Custom Panel: The VanishingPointPanel 56 Building a Panel with Attached Properties: WeightedPanel 58 Using Transformations with Layout 63 Enter the LayoutTransform 66 Layout Events 66 Summary 68 Table of Contents v From the Library of Lee Bogdanoff www.it-ebooks.info ptg 5 Using Existing Controls 69 Customizing Existing Controls 70 Customizing Controls Using Properties 70 Customization Using Control Templates 70 Customization with Data Templates 71 Using a ControlTemplate and a DataTemplate 71 Customizing the ItemsControl 72 Customizing a ListBox 74 Customizing the ItemContainerStyle 74 Customizing the ItemTemplate and the ItemsPanelTemplate 77 Creating a Custom ScrollBar 78 Using Brushes to Create Advanced Visuals 82 Using the VisualTreeHelper and LogicalTreeHelper 82 Customization Sample—The Radar Screen 83 Moving Enemies in a ListBox 84 Concentric Circles and a Sweeping Cone 85 Summary 91 6 The Power of Attached Properties 93 Overview of Attached Properties 93 Building the UseHover Attached Property 95 Using Attached Properties as Extension Points 100 Data Templates 102 Property Abstraction 102 Layout Animations 102 Constraining Panels 102 Application Services 102 UI Helper Objects 103 Implementing Drag and Drop with Attached Properties 103 Summary 111 Part II Adding Complex Features 7 Advanced Scrolling 113 The Anatomy of a Scrollbar 113 The Magic of IScrollInfo 115 Responding to User-Requested Horizontal and Vertical Scrolling 116 WPF Control Development Unleashed vi From the Library of Lee Bogdanoff www.it-ebooks.info ptg Controlling the Bounds for the Track and Thumb 116 Managing the Location of the Thumb 116 Logical Scrolling 117 Building a Custom Panel with Custom Scrolling 117 Creating the Layout Logic 117 Adding the Scrolling Functionality 119 Animated Scrolling 122 Taking Scrolling to the Next Step 123 Scrolling Without IScrollInfo 124 Summary 127 8 Virtualization 129 Virtualization Distilled 129 Building Blocks of UI Virtualization 130 UI Virtualization in WPF 131 Component Interaction 132 A Deeper Look at the ItemContainerGenerator 133 Making Our Own Virtualized Control: The StaggeredPanel 135 Deferred Scrolling 139 Container Recycling 140 Virtualization in 3D 140 Summary 142 9 Creating Advanced Controls and Visual Effects 143 Lasso Selection Using the InkCanvas 143 Building a Dock Slide Presenter 146 Docking and Undocking Controls 149 Building a Transition Abstraction: The TransitionContainer 154 Handling Transitions 157 Applying a Transition 159 Implementing Popular Visual Effects 161 Reflection 161 Drop Shadows 163 Opacity Masks 164 Gloss Effects 164 Summary 166 Table of Contents vii From the Library of Lee Bogdanoff www.it-ebooks.info ptg 10 Control Skinning and Themes 167 Introduction to Skins and Themes 168 Resource Lookups in WPF 168 Building Default Styles 169 Using Resources in Default Styles 170 Creating Theme-Specific Styles 172 Enabling Runtime Skinning 174 Using the ApplyTemplate Override 177 Control Customization Through Property Exposure 182 Summary 183 Part III Building Interactivity, 3D, Animations 11 Bridging the 2D and 3D Worlds 185 A Brief Introduction to 3D Worlds 185 Using the Viewport3D Element 186 Embedding a Viewport3D Element 189 Mapping 2D Visuals on 3D Surfaces 192 Getting Interactive with ModelUIElement3D and ContainerUIElement3D 196 2D Bounds of a 3D Object 198 Hints on Layout in 3D 200 Interactive 2D-on-3D Surfaces 200 Summary 201 12 Custom Animations 203 Procedural Animations 203 Animating Using the DispatcherTimer 204 Animating Using CompositionTarget.Rendering 204 Animating with Storyboards 206 Simple Type-Based Animations (From, To, and By) 206 Keyframe Animations 207 Using Storyboards with Parallel Timelines 208 Using Path-Based Animations 211 Creating Custom Animations 212 Creating the 3D Surfaces 219 WPF Control Development Unleashed viii From the Library of Lee Bogdanoff www.it-ebooks.info ptg Animating Within the DrawingContext 220 Summary 221 13 Pixel Shader Effects 223 New and Improved Bitmap Effects 224 Working with Shaders 224 Setting Up the Environment 224 An Overview of HLSL 225 Writing Custom Shaders 228 Grayscale Shader 228 Building a Parameterized Shader: TwirlEffect 231 Animating the Shader Effects 235 Effect Mapping for GUI Interaction and Eventing 235 Multi-Input Shaders 239 A Useful Tool 242 Summary 242 Part IV Bringing the Controls to the Real World 14 Events, Commands, and Focus 243 Routed Events 243 Routed Events, Event Triggers, and Property Mirrors 245 Attached Events 246 Class Handlers 249 Weak Events Using Weak References 250 Implementing the Weak Event Pattern 251 Subclassing the Weak Event Manager 252 Delivering Events Via the IWeakEventListener 254 Commands 255 Routed Commands 259 Commands Versus Events 259 Request Requery 261 The ICommandSource Interface 262 Focus Management 266 Logical and Keyboard Focus 266 Focus-Related Events and Properties 267 Keyboard Navigation 271 Summary 273 Table of Contents ix From the Library of Lee Bogdanoff www.it-ebooks.info [...]... any UI control that displays data really needs to be able to handle any kind of data This is the crux of why so many UI frameworks have historically been so difficult to use and why WPF is so powerful Many WPF controls assume that the default data type of its content is System.Object If a control s content can be of any data type, how do controls know how to render their own content? How does WPF take... Lee Bogdanoff 4 CHAPTER 1 The WPF Design Philosophy WPF provides two different kinds of templates that operate on two different kinds of cookie dough control templates and data templates A control template is used when WPF wants a visual representation of a control This template is completely related to the user interface and generally does not involve any data The default control template for a button... a single line of Windows Presentation Foundation (WPF) code or markup, we want to make sure that you know why WPF applications work the way they do and that you know how the myriad moving parts in a WPF application interact with each other to produce what the end user experiences and manipulates Knowing the WPF design philosophy will make your future WPF applications more reliable, more robust, and... ItemsPanelTemplate UIElement ContainerVisual DrawingVisual FrameworkElement Control ContentControl UserControl ItemsControl ContentPresenter Panel Decorator Shape TextBlock Adorner Image InkCanvas MediaElement Viewport3D FIGURE 2.1 The visual classes creating next-generation applications What follows is a brief overview of the most important classes in WPF These are the classes that you will use most often as you... from raw data to rendered output Windows Forms allowed us to often drag and drop individual controls and quickly point that control at the underlying data to render it However, building custom controls in Windows Forms was a complex task and required the skills of a Graphics Device Interface (GDI) Zen master WPF strikes at the sweet spot and by giving developers flexibility while still making it easy... much everything else in between He is currently a NET Architect in New England building large-scale, next-generation web applications www.it-ebooks.info From the Library of Lee Bogdanoff xiv WPF Control Development Unleashed Dedications Pavan: I dedicate this book to my parents, my grandparents, my brother, and my loving wife Sirisha, who seeded the idea of writing a book Kevin: I would like to dedicate... and flow that through a control hierarchy and eventually convert that into something visible, tangible, and responsive to user interactions? The first answer is through the use of templates Templates Think of templates as cookie cutters Whenever WPF needs a new cookie, it asks a template for one The template then stamps a nice fresh shape out of the dough and gives it to WPF WPF then bakes up the cookie... freedom to explore WPF and go crazy with ideas I would like to extend my thanks to all the bloggers, book authors, article writers, community contributors, and speakers who have shared their knowledge on WPF and software development, enriching this field with great ideas and practices They have made programming all the more fun Finally, huge props go to everyone on the Microsoft WPF team for building...x WPF Control Development Unleashed 15 Advanced Data Binding 275 Dependency Properties 276 Dependency Property Precedence ... with many leading UI technologies, including WPF/ Silverlight, Flash/Flex/AIR, and DHTML In the past, he has worked with Java Swing, Eclipse SWT, and TrollTech/Nokia Qt His primary interests include 2D/3D graphics, data visualization, UI architecture, and computational art He created FluidKit (http://fluidkit.codeplex.com), an open-source WPF library of controls such as ElementFlow, TransitionPresenter, . Existing Controls 69 Customizing Existing Controls 70 Customizing Controls Using Properties 70 Customization Using Control Templates 70 Customization with Data Templates 71 Using a ControlTemplate. User-Requested Horizontal and Vertical Scrolling 116 WPF Control Development Unleashed vi From the Library of Lee Bogdanoff www.it-ebooks.info ptg Controlling the Bounds for the Track and Thumb 116 Managing. the ropes of WPF, I got the chance to do a project with Andrew Whiddett, a veteran at the art of building jaw-dropping user interfaces. Since then he has WPF Control Development Unleashed xiv From

Ngày đăng: 24/04/2014, 16:27

TỪ KHÓA LIÊN QUAN