logo资料库

WPF编程宝典 英文pdf版 C#语言.pdf

第1页 / 共1201页
第2页 / 共1201页
第3页 / 共1201页
第4页 / 共1201页
第5页 / 共1201页
第6页 / 共1201页
第7页 / 共1201页
第8页 / 共1201页
资料共1201页,剩余部分请下载后查看
1430272058
Title Page
Copyright Page
Table of Contents
About the Author
About the Technical Reviewer
Acknowledgments
Introduction
About This Book
Chapter Overview
What You Need to Use This Book
Code Samples and URLs
Feedback
CHAPTER 1 Introducing WPF
The Evolution of Windows Graphics
DirectX: The New Graphics Engine
Hardware Acceleration and WPF
WPF: A Higher-Level API
Windows Forms Lives On
DirectX Also Lives On
Silverlight
Resolution Independence
WPF Units
System DPI
Windows XP
Windows Vista
Windows 7
Bitmap and Vector Graphics
The Architecture of WPF
The Class Hierarchy
System.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Media.Visual
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Shapes.Shape
System.Windows.Controls.Control
System.Windows.Controls.ContentControl
System.Windows.Controls.ItemsControl
System.Windows.Controls.Panel
WPF 4
New Features
The WPF Toolkit
Visual Studio 2010
Multitargeting
The .NET Client Profile
The Visual Studio Designer
The Last Word
CHAPTER 2 XAML
Understanding XAML
Graphical User Interfaces Before WPF
The Variants of XAML
XAML Compilation
XAML Basics
XAML Namespaces
The Code-Behind Class
The InitializeComponent() Method
Naming Elements
Properties and Events in XAML
Simple Properties and Type Converters
Complex Properties
Markup Extensions
Attached Properties
Nesting Elements
Special Characters and Whitespace
Events
The Full Eight Ball Example
Using Types from Other Namespaces
Loading and Compiling XAML
Code-Only
Code and Uncompiled XAML
Code and Compiled XAML
XAML Only
XAML 2009
Automatic Event Hookup
References
Built-in Types
Advanced Object Creation
The Last Word
CHAPTER 3 Layout
Understanding Layout in WPF
The WPF Layout Philosophy
The Layout Process
The Layout Containers
Simple Layout with the StackPanel
Layout Properties
Alignment
Margin
Minimum, Maximum, and Explicit Sizes
The Border
The WrapPanel and DockPanel
The WrapPanel
The DockPanel
Nesting Layout Containers
The Grid
Fine-Tuning Rows and Columns
Layout Rounding
Spanning Rows and Columns
Split Windows
Shared Size Groups
The UniformGrid
Coordinate-Based Layout with the Canvas
Z-Order
The InkCanvas
Layout Examples
A Column of Settings
Dynamic Content
A Modular User Interface
The Last Word
CHAPTER 4 Dependency Properties
Understanding Dependency Properties
Defining a Dependency Property
Registering a Dependency Property
Adding a Property Wrapper
How WPF Uses Dependency Properties
Shared Dependency Properties
Attached Dependency Properties
Property Validation
The Validation Callback
The Coercion Callback
The Last Word
CHAPTER 5 Routed Events
Understanding Routed Events
Defining, Registering, and Wrapping a Routed Event
Sharing Routed Events
Raising a Routed Event
Handling a Routed Event
Event Routing
The RoutedEventArgs Class
Bubbling Events
Handling a Suppressed Event
Attached Events
Tunneling Events
WPF Events
Lifetime Events
Input Events
Keyboard Input
Handling a Key Press
Focus
Getting Key State
Mouse Input
Mouse Clicks
Capturing the Mouse
Drag-and-Drop
Multitouch Input
The Levels of Multitouch Support
Raw Touch
Manipulation
Inertia
The Last Word
CHAPTER 6 Controls
The Control Class
Background and Foreground Brushes
Setting Colors in Code
Setting Colors in XAML
Font Family
Text Decorations and Typography
Font Inheritance
Font Substitution
Font Embedding
Text Formatting Mode
Mouse Cursors
Content Controls
The Content Property
Aligning Content
The WPF Content Philosophy
Labels
Buttons
The Button
The ToggleButton and RepeatButton
The CheckBox
The RadioButton
Tooltips
Setting ToolTip Properties
Setting ToolTipService Properties
The Popup
Specialized Containers
The ScrollViewer
Programmatic Scrolling
Custom Scrolling
Headered Content Controls
The GroupBox
The TabItem
The Expander
Text Controls
Multiple Lines of Text
Text Selection
Spell Checking
The PasswordBox
List Controls
The ListBox
The ComboBox
Range-Based Controls
The Slider
The ProgressBar
Date Controls
The Last Word
CHAPTER 7 The Application
The Application Life Cycle
Creating an Application Object
Deriving a Custom Application Class
Application Shutdown
Application Events
Application Tasks
Showing a Splash Screen
Handling Command-Line Arguments
Accessing the Current Application
Interacting Between Windows
Single-Instance Applications
Creating the Single-Instance Application Wrapper
Registering the File Type
Assembly Resources
Adding Resources
Retrieving Resources
Resource-Aware Classes
Pack URIs
Resources in Other Assemblies
Content Files
Localization
Building Localizable User Interfaces
Preparing an Application for Localization
The Translation Process
Preparing Markup Elements for Localization
Extracting Localizable Content
Building a Satellite Assembly
The Last Word
CHAPTER 8 Element Binding
Binding Elements Together
The Binding Expression
Binding Errors
Binding Modes
OneWayToSource
Default
Creating Bindings with Code
Multiple Bindings
Binding Updates
Binding to Objects That Aren’t Elements
Source
RelativeSource
DataContext
The Last Word
CHAPTER 9 Commands
Understanding Commands
The WPF Command Model
The ICommand Interface
The RoutedCommand Class
The RoutedUICommand Class
The Command Library
Executing Commands
Command Sources
Command Bindings
Using Multiple Command Sources
Fine-Tuning Command Text
Invoking a Command Directly
Disabling Commands
Controls with Built-in Commands
Advanced Commands
Custom Commands
Using the Same Command in Different Places
Using a Command Parameter
Tracking and Reversing Commands
The Last Word
CHAPTER 10 Resources
Resource Basics
The Resources Collection
The Hierarchy of Resources
Static and Dynamic Resources
Nonshared Resources
Accessing Resources in Code
Application Resources
System Resources
Resource Dictionaries
Creating a Resource Dictionary
Using a Resource Dictionary
Sharing Resources Between Assemblies
The Last Word
CHAPTER 11 Styles and Behaviors
Style Basics
Creating a Style Object
Setting Properties
Attaching Event Handlers
The Many Layers of Styles
Automatically Applying Styles by Type
Triggers
A Simple Trigger
Behaviors
Getting Support for Behaviors
Understanding the Behavior Model
Creating a Behavior
Using a Behavior
Design-Time Behavior Support in Blend
The Last Word
CHAPTER 12 Shapes, Brushes, and Transforms
Understanding Shapes
The Shape Classes
Rectangle and Ellipse
Sizing and Placing Shapes
Scaling Shapes with a Viewbox
Line
Polyline
Polygon
Line Caps and Line Joins
Dashes
Pixel Snapping
Brushes
The SolidColorBrush
The LinearGradientBrush
The RadialGradientBrush
The ImageBrush
A Tiled ImageBrush
The VisualBrush
The BitmapCacheBrush
Transforms
Transforming Shapes
Transforming Elements
Transparency
Making an Element Partially Transparent
Opacity Masks
The Last Word
CHAPTER 13 Geometries and Drawings
Paths and Geometries
Line, Rectangle, and Ellipse Geometries
Combining Shapes with GeometryGroup
Fusing Geometries with CombinedGeometry
Curves and Lines with PathGeometry
Straight Lines
Arcs
Bézier Curves
The Geometry Mini-Language
Clipping with Geometry
Drawings
Displaying a Drawing
Exporting Clip Art
The Last Word
CHAPTER 14 Effects and Visuals
Visuals
Drawing Visuals
Wrapping Visuals in an Element
Hit Testing
Complex Hit Testing
Effects
BlurEffect
DropShadowEffect
ShaderEffect
The WriteableBitmap Class
Generating a Bitmap
Writing to a WriteableBitmap
More Efficient Pixel Writing
The Last Word
CHAPTER 15 Animation Basics
Understanding WPF Animation
Timer-Based Animation
Property-Based Animation
Basic Animation
The Animation Classes
Animations in Code
From
To
By
Duration
Simultaneous Animations
Animation Lifetime
The Timeline Class
AccelerationRatio and DecelerationRatio
RepeatBehavior
Storyboards
The Storyboard
Event Triggers
Attaching Triggers with a Style
Overlapping Animations
Synchronized Animations
Controlling Playback
Monitoring Progress
Animation Easing
Using an Easing Function
Easing In and Easing Out
Easing Function Classes
Creating a Custom Easing Function
Animation Performance
Desired Frame Rate
Bitmap Caching
The Last Word
CHAPTER 16 Advanced Animation
Animation Types Revisited
Animating Transforms
Animating Multiple Transforms
Animating Brushes
VisualBrush
Animating Pixel Shaders
Key Frame Animation
Discrete Key Frame Animations
Easing Key Frames
Spline Key Frame Animations
Path-Based Animation
Frame-Based Animation
Storyboards in Code
The Main Window
The Bomb User Control
Dropping the Bombs
Intercepting a Bomb
Counting Bombs and Cleaning Up
The Last Word
CHAPTER 17 Control Templates
Understanding Logical Trees and Visual Trees
Understanding Templates
The Chrome Classes
Dissecting Controls
Creating Control Templates
A Simple Button
Template Bindings
Triggers That Change Properties
Triggers That Use Animation
Organizing Template Resources
Refactoring the Button Control Template
Applying Templates with Styles
Applying Templates Automatically
User-Selected Skins
Building More Complex Templates
Nested Templates
Modifying the Scroll Bar
The Control Template Examples
Visual States
The Last Word
CHAPTER 18 Custom Elements
Understanding Custom Elements in WPF
Building a Basic User Control
Defining Dependency Properties
Defining Routed Events
Adding Markup
Using the Control
Command Support
More Robust Commands
A Closer Look at User Controls
Creating a Lookless Control
Refactoring the Color Picker Code
Refactoring the Color Picker Markup
Streamlining the Control Template
Adding Part Names
Manipulating Template Parts
Documenting Template Parts
Theme-Specific Styles and the Default Style
Supporting Visual States
Starting the FlipPanel Class
Choosing Parts and States
The Default Control Template
The Flip Button
Defining the State Animations
Defining the State Transitions
Wiring Up the Elements
Using the FlipPanel
Using a Different Control Template
Custom Panels
The Two-Step Layout Process
MeasureOverride()
ArrangeOverride()
The Canvas Clone
A Better Wrapping Panel
Custom-Drawn Elements
The OnRender() Method
Evaluating Custom Drawing
A Custom-Drawn Element
A Custom Decorator
The Last Word
CHAPTER 19 Data Binding
Binding to a Database with Custom Objects
Building a Data Access Component
Building a Data Object
Displaying the Bound Object
Updating the Database
Change Notification
Binding to a Collection of Objects
Displaying and Editing Collection Items
Inserting and Removing Collection Items
Binding to the ADO.NET Objects
Binding to a LINQ Expression
Improving Performance in Large Lists
Virtualization
Item Container Recycling
Deferred Scrolling
Validation
Validation in the Data Object
The ExceptionValidationRule
The DataErrorValidationRule
Custom Validation Rules
Reacting to Validation Errors
Getting a List of Errors
Showing a Different Error Indicator
Validating Multiple Values
Data Providers
The ObjectDataProvider
Error Handling
Asynchronous Support
The XmlDataProvider
The Last Word
CHAPTER 20 Formatting Bound Data
Data Binding Redux
Data Conversion
The StringFormat Property
Introducing Value Converters
Formatting Strings with a Value Converter
Creating Objects with a Value Converter
Applying Conditional Formatting
Evaluating Multiple Properties
List Controls
List Styles
The ItemContainerStyle
A ListBox with Check Boxes or Radio Buttons
Alternating Item Style
Style Selectors
Data Templates
Separating and Reusing Templates
More Advanced Templates
Varying Templates
Template Selectors
Templates and Selection
Changing Item Layout
The ComboBox
The Last Word
CHAPTER 21 Data Views
The View Object
Retrieving a View Object
Navigating with a View
Creating a View Declaratively
Filtering, Sorting, and Grouping
Filtering Collections
Filtering the DataTable
Sorting
Grouping
The Last Word
CHAPTER 22 Lists, Grids, and Trees
The ListView
Creating Columns with the GridView
Resizing Columns
Cell Templates
Creating a Custom View
The View Class
The View Styles
Using the ListView
Passing Information to a View
The TreeView
A Data-Bound TreeView
Binding a DataSet to a TreeView
Just-in-Time Node Creation
The DataGrid
Resizing and Rearranging Columns
Defining Columns
The DataGridCheckBoxColumn
DataGridHyperlinkColumn
DataGridComboBoxColumn
The DataGridTemplateColumn
Formatting and Styling Columns
Formatting Rows
Row Details
Freezing Columns
Selection
Sorting
DataGrid Editing
The Last Word
CHAPTER 23 Windows
The Window Class
Showing a Window
Positioning a Window
Saving and Restoring Window Location
Window Interaction
Window Ownership
The Dialog Model
Common Dialog Boxes
Nonrectangular Windows
A Simple Shaped Window
A Transparent Window with Shaped Content
Moving Shaped Windows
Resizing Shaped Windows
Putting It All Together: A Custom Control Template for Windows
The Aero Glass Effect
Programming the Windows 7 Taskbar
Using Jump Lists
Recent Document Support
Custom Jump Lists
Creating Jump List Items in Code
Launching Application Tasks from the Jump List
Changing the Taskbar Icon and Preview
Thumbnail Clipping
Thumbnail Buttons
Progress Notification
Overlay Icons
The Last Word
CHAPTER 24 Pages and Navigation
Understanding Page-Based Navigation
Page-Based Interfaces
A Simple Page-Based Application with NavigationWindow
The Page Class
Hyperlinks
Navigating to Websites
Fragment Navigation
Hosting Pages in a Frame
Hosting Pages in Another Page
Hosting Pages in a Web Browser
The Page History
A Closer Look at URIs in WPF
Navigation History
Maintaining Custom Properties
The Navigation Service
Programmatic Navigation
Navigation Events
Managing the Journal
Adding Custom Items to the Journal
Page Functions
XAML Browser Applications
XBAP Requirements
Creating an XBAP
Deploying an XBAP
Updating an XBAP
XBAP Security
Full-Trust XBAPs
Combination XBAP/Stand-Alone Applications
Coding for Different Security Levels
Isolated Storage
Simulating Dialog Boxes with the Popup Control
Embedding an XBAP in a Web Page
The WebBrowser Control
Navigating to a Page
Building a DOM Tree
Scripting a Web Page with .NET Code
The Last Word
CHAPTER 25 Menus, Toolbars, and Ribbons
Menus
The Menu Class
Menu Items
The ContextMenu Class
Menu Separators
Toolbars and Status Bars
The ToolBar
The Overflow Menu
The ToolBarTray
The StatusBar
Ribbons
Adding the Ribbon
Styling the Ribbon
Commands
The Application Menu
Tabs, Groups, and Buttons
Ribbon Sizing
The Quick access Toolbar
The Last Word
CHAPTER 26 Sound and Video
Playing WAV Audio
The SoundPlayer
The SoundPlayerAction
System Sounds
The MediaPlayer
The MediaElement
Playing Audio Programmatically
Handling Errors
Playing Audio with Triggers
Playing Multiple Sounds
Changing Volume, Balance, Speed, and Position
Synchronizing an Animation with Audio
Playing Video
Video Effects
Speech
Speech Synthesis
Speech Recognition
The Last Word
CHAPTER 27 3-D Drawing
3-D Drawing Basics
The Viewport
3-D Objects
Geometry
Geometry Model and Surfaces
Light Sources
The Camera
Deeper into 3-D
Shading and Normals
More Complex Shapes
Model3DGroup Collections
Materials Revisited
Texture Mapping
Mapping the ImageBrush
Video and the VisualBrush
Interactivity and Animations
Transforms
Rotations
A Fly Over
The Trackball
Hit Testing
Hit Testing in the Viewport
The ModelUIElement3D
Hit Testing with the ModelUIElement3D
The ContainerUIElement3D
2-D Elements on 3-D Surfaces
The Last Word
CHAPTER 28 Documents
Understanding Documents
Flow Documents
The Flow Elements
Formatting Content Elements
Constructing a Simple Flow Document
Block Elements
Paragraph and Run
List
Table
Section
BlockUIContainer
Inline Elements
Preserving Whitespace
Floater
Figure
Interacting with Elements Programmatically
Text Justification
Read-Only Flow Document Containers
Zooming
Pages and Columns
Loading Documents from a File
Printing
Editing a Flow Document
Loading a File
Saving a File
Formatting Selected Text
Getting Individual Words
Fixed Documents
Annotations
The Annotation Classes
Enabling the Annotation Service
Creating Annotations
Examining Annotations
Reacting to Annotation Changes
Storing Annotations in a Fixed Document
Customizing the Appearance of Sticky Notes
The Last Word
CHAPTER 29 Printing
Basic Printing
Printing an Element
Transforming Printed Output
Printing Elements Without Showing Them
Printing a Document
Manipulating the Pages in a Document Printout
Custom Printing
Printing with the Visual Layer Classes
Custom Printing with Multiple Pages
Print Settings and Management
Maintaining Print Settings
Printing Page Ranges
Managing a Print Queue
Printing Through XPS
Creating an XPS Document for a Print Preview
Writing to an In-Memory XPS Document
Printing Directly to the Printer via XPS
Asynchronous Printing
The Last Word
CHAPTER 30 Interacting with Windows Forms
Assessing Interoperability
Missing Features in WPF
Mixing Windows and Forms
Adding Forms to a WPF Application
Adding WPF Windows to a Windows Forms Application
Showing Modal Windows and Forms
Showing Modeless Windows and Forms
Visual Styles for Windows Forms Controls
Windows Forms Classes That Don’t Need Interoperability
Creating Windows with Mixed Content
WPF and Windows Forms “Airspace”
Hosting Windows Forms Controls in WPF
WPF and Windows Forms User Controls
Hosting WPF Controls in Windows Forms
Access Keys, Mnemonics, and Focus
Property Mapping
The Last Word
CHAPTER 31 Multithreading
Multithreading
The Dispatcher
The DispatcherObject
The BackgroundWorker
A Simple Asynchronous Operation
Creating the BackgroundWorker
Running the BackgroundWorker
Tracking Progress
Supporting Cancellation
The Last Word
CHAPTER 32 The Add-in Model
Choosing Between MAF and MEF
The Add-in Pipeline
How the Pipeline Works
The Add-in Folder Structure
Preparing a Solution That Uses the Add-in Model
An Application That Uses Add-Ins
The Contract
The Add-in View
The Add-In
The Add-in Adapter
The Host View
The Host Adapter
The Host
Adding More Add-Ins
Interacting with the Host
Visual Add-Ins
The Last Word
CHAPTER 33 ClickOnce Deployment
Understanding Application Deployment
The ClickOnce Installation Model
ClickOnce Limitations
A Simple ClickOnce Publication
Setting the Publisher and Production
Starting the Publish Wizard
Publishing for a Network File Share
Publishing for a Web Server
Publishing for a CD or DVD
Online or Offline
The Deployed File Structure
Installing a ClickOnce Application
Updating a ClickOnce Application
Additional ClickOnce Options
Publish Version
Updates
File Associations
Publish Options
The Last Word
Index
fulltext
Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4.0 ■ ■ ■ Matthew MacDonald
■ CONTENTS Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4.0 Copyright © 2010 by Matthew MacDonald All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-7205-2 ISBN-13 (electronic): 978-1-4302-7204-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Publisher and President: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Fabio Claudio Ferracchiati Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Anne Collett Copy Editor: Marilyn Smith and Kim Wimpsett Compositor: ContentWorks, Inc. and Bob Cooper Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales—eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii
■ CONTENTS For my wonderful family, Faria, Maya, and Brenna iii
■ CONTENTS Contents ■Chapter 1: Introducing WPF ............................................................................................................... 1 The Evolution of Windows Graphics ................................................................................................ 1 DirectX: The New Graphics Engine ............................................................................................. 2 Hardware Acceleration and WPF ................................................................................................ 3 WPF: A Higher-Level API ................................................................................................................ 4 Windows Forms Lives On ........................................................................................................... 6 DirectX Also Lives On ................................................................................................................. 6 Silverlight ................................................................................................................................... 6 Resolution Independence ............................................................................................................... 7 WPF Units .................................................................................................................................. 8 System DPI ................................................................................................................................ 9 Bitmap and Vector Graphics ..................................................................................................... 12 The Architecture of WPF ............................................................................................................... 12 The Class Hierarchy ................................................................................................................. 14 WPF 4 .......................................................................................................................................... 17 New Features ........................................................................................................................... 17 The WPF Toolkit ....................................................................................................................... 18 Visual Studio 2010 ................................................................................................................... 18 The Last Word .............................................................................................................................. 21 ■Chapter 2: XAML .............................................................................................................................. 23 Understanding XAML .................................................................................................................... 24 Graphical User Interfaces Before WPF ...................................................................................... 24 The Variants of XAML ............................................................................................................... 25 XAML Compilation .................................................................................................................... 26 XAML Basics ................................................................................................................................ 27 XAML Namespaces .................................................................................................................. 28 iv
■ CONTENTS The Code-Behind Class ............................................................................................................ 30 Properties and Events in XAML ..................................................................................................... 32 Simple Properties and Type Converters .................................................................................... 34 Complex Properties .................................................................................................................. 35 Markup Extensions ................................................................................................................... 37 Attached Properties .................................................................................................................. 38 Nesting Elements ..................................................................................................................... 39 Special Characters and Whitespace ......................................................................................... 42 Events ...................................................................................................................................... 44 The Full Eight Ball Example ...................................................................................................... 45 Using Types from Other Namespaces ........................................................................................... 46 Loading and Compiling XAML ....................................................................................................... 48 Code-Only ................................................................................................................................ 48 Code and Uncompiled XAML .................................................................................................... 51 Code and Compiled XAML ........................................................................................................ 53 XAML Only ............................................................................................................................... 55 XAML 2009 .................................................................................................................................. 56 Automatic Event Hookup .......................................................................................................... 56 References............................................................................................................................... 58 Built-in Types ........................................................................................................................... 58 Advanced Object Creation ........................................................................................................ 59 The Last Word .............................................................................................................................. 60 ■Chapter 3: Layout ............................................................................................................................. 61 Understanding Layout in WPF ....................................................................................................... 61 The WPF Layout Philosophy ..................................................................................................... 62 The Layout Process .................................................................................................................. 63 The Layout Containers ............................................................................................................. 63 Simple Layout with the StackPanel ............................................................................................... 65 Layout Properties ..................................................................................................................... 67 Alignment ................................................................................................................................ 68 Margin ..................................................................................................................................... 69 Minimum, Maximum, and Explicit Sizes ................................................................................... 71 v
■ CONTENTS The Border ............................................................................................................................... 74 The WrapPanel and DockPanel ..................................................................................................... 75 The WrapPanel ......................................................................................................................... 76 The DockPanel ......................................................................................................................... 77 Nesting Layout Containers ....................................................................................................... 79 The Grid ....................................................................................................................................... 80 Fine-Tuning Rows and Columns ............................................................................................... 83 Layout Rounding ...................................................................................................................... 85 Spanning Rows and Columns ................................................................................................... 86 Split Windows .......................................................................................................................... 87 Shared Size Groups .................................................................................................................. 91 The UniformGrid ....................................................................................................................... 93 Coordinate-Based Layout with the Canvas ................................................................................... 94 Z-Order .................................................................................................................................... 95 The InkCanvas ......................................................................................................................... 96 Layout Examples .......................................................................................................................... 99 A Column of Settings ................................................................................................................ 99 Dynamic Content ................................................................................................................... 100 A Modular User Interface ........................................................................................................ 102 The Last Word ............................................................................................................................ 104 ■Chapter 4: Dependency Properties ................................................................................................ 105 Understanding Dependency Properties ....................................................................................... 105 Defining a Dependency Property ............................................................................................ 106 Registering a Dependency Property ........................................................................................ 107 Adding a Property Wrapper .................................................................................................... 109 How WPF Uses Dependency Properties .................................................................................. 110 Shared Dependency Properties .............................................................................................. 111 Attached Dependency Properties ............................................................................................ 112 Property Validation ..................................................................................................................... 114 The Validation Callback .......................................................................................................... 114 The Coercion Callback ........................................................................................................... 115 The Last Word ............................................................................................................................ 118 vi
■ CONTENTS ■Chapter 5: Routed Events .............................................................................................................. 119 Understanding Routed Events ..................................................................................................... 119 Defining, Registering, and Wrapping a Routed Event .............................................................. 120 Sharing Routed Events ........................................................................................................... 121 Raising a Routed Event .......................................................................................................... 121 Handling a Routed Event ........................................................................................................ 121 Event Routing ............................................................................................................................. 123 The RoutedEventArgs Class .................................................................................................... 125 Bubbling Events ..................................................................................................................... 126 Handling a Suppressed Event ................................................................................................. 129 Attached Events ..................................................................................................................... 129 Tunneling Events ................................................................................................................... 131 WPF Events ................................................................................................................................ 133 Lifetime Events ...................................................................................................................... 133 Input Events ........................................................................................................................... 136 Keyboard Input ........................................................................................................................... 137 Handling a Key Press ............................................................................................................. 138 Focus ..................................................................................................................................... 140 Getting Key State ................................................................................................................... 141 Mouse Input ............................................................................................................................... 143 Mouse Clicks ......................................................................................................................... 144 Capturing the Mouse .............................................................................................................. 146 Drag-and-Drop ...................................................................................................................... 146 Multitouch Input ......................................................................................................................... 149 The Levels of Multitouch Support ........................................................................................... 150 Raw Touch ............................................................................................................................. 150 Manipulation .......................................................................................................................... 153 Inertia .................................................................................................................................... 156 The Last Word ............................................................................................................................ 157 ■Chapter 6: Controls ........................................................................................................................ 159 The Control Class ....................................................................................................................... 160 Background and Foreground Brushes ..................................................................................... 160 vii
分享到:
收藏