SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
WPF Control Development Unleashed
Building Advanced User Experiences
In this book, two leading Windows Presentation Foundation experts give developers everything they need to build next-generation WPF applications–software that is more robust, usable, and compelling.
Drawing on their close ties with Microsoft’s WPF development team, Pavan Podila and Kevin Hoffman give you a clear, robust, and practical understanding of WPF, its underpinnings, its overall architecture, and its design philosophy. Podila and Hoffman introduce never-before-published WPF design patterns and support them with robust, real-world code examples–all presented in full color, just as they appear in Visual Studio.
The authors begin by explaining how to “think in WPF,” and then introduce powerful new techniques for everything from handling 3D layouts to creating game-like physics effects. Along the way, they offer in-depth coverage of data binding, building interactivity, and control development: three of WPF’s most challenging concepts. You’ll learn how to choose the right WPF features for every programming challenge, and use those features far more creatively and effectively.
If you want to build truly outstanding WPF applications, this is the book that will get you there.
Category: .NET Programming / WPF
Covers: Windows Presentation Foundation
User Level: Intermediate—Advanced
WPF Control Development: The Diverse Visual Class Structure
Download the sample pages (includes Chapter 2 and Index)
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
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
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
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
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
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
15 Advanced Data Binding 275
Dependency Properties 276
Dependency Property Precedence 276
Using AddOwner Instead of Creating a New DependencyProperty 279
Listening to Property Changed Events on Dependency Properties 280
Special Data Binding Scenarios 282
Using RelativeSource.PreviousData 282
Using NotifyOnSourceUpdated and NotifyOnTargetUpdated 284
The Dispatcher and DispatcherPriority 285
Deferring UI Operations 287
Posting Messages from Worker Threads 287
The BackgroundWorker Class 289
Introduction to Continuous LINQ (CLINQ) 291
Summary 292
16 Control and Visual Design Tips 295
Control Design Tips 295
Use Internal Resource Dictionaries 295
Define Complex Controls as Partial Classes 296
Use Scoped Dependency Properties for Internal State Management 296
Use Attached Properties for Adding Functionality 297
Compose Graphics Using Simpler Building Blocks 297
Communicating Between a Control and Its Subparts 297
Use a State Machine to Handle Multiple Events and Property Changes 299
Use Low-Priority Delegates for Noncritical Tasks 299
Use x: Shared for Cloning Resources 299
Use Markup Extensions to Encapsulate Object Creation 300
Useful Patterns for GUI Development 301
The Strategy Pattern 301
The Builder Pattern 301
Model-View-Controller 302
Model-View-View Model 302
Factory Method 303
Composed Method 303
State Pattern 303
Code Should be Idiomatic with Regard to“Framework Design Guidelines” 304
Visual Design Tips 304
Using Tile Brushes 304
Using Gradients with Relative Transforms 306
XAML Coding Conventions 307
Use the Vista Interface Guidelines 308
Using Nonstandard Fonts for Icons 308
Using Transparent PNGs 309
Import from Photoshop and Illustrator 309
Opacity Masks 310
Using Clip Geometries 310
Some Useful Tools 312
Snoop 312
Mole 313
Kaxaml 313
Summary 315
17 Performance 317
Different Kinds of Performance 318
Choice of Visuals 318
Brushes and Caching 320
Resource Management 321
Reference Handling 321
Data Binding and Freezables 321
Background Threads for Long-Running Operations 322
Scrolling and Virtualization 322
Storyboard Animations 323
Pixel Shaders 323
Framework Property Metadata Options 323
RenderCapability–Hardware and Software Rendering 324
Optimizing the Render Pipeline 325
3D 325
Measuring Performance 326
Visual Profiler 327
Perforator 328
Third-Party Tools 329
Perceived Responsiveness 329
Summary 330
18 Control Automation 331
The Need for UI Automation 331
The Automation Object Model 332
Assemblies and Namespaces 333
AutomationElement, AutomationPeer, and Control Patterns 333
Automation Properties 335
Navigating the Automation Tree 336
Using the Automation API 338
Locating Elements in the Automation Tree 339
Checking for Control Patterns 340
Looking Up Property Values 341
Listening to Events 341
Navigating to the Parent, Child, or Sibling 342
Performing Operations on Automation Elements 342
Automation of a Custom Control 343
Picking a Base Peer Class 343
Picking Pattern Interfaces, aka the Control Patterns 344
Building RangeSelectorAutomationPeer 345
Additional Resources 349
Summary 349
Index 351