You are previewing Programming WPF, 2nd Edition.
O'Reilly logo
Programming WPF, 2nd Edition

Book Description

If you want to build applications that take full advantage of Windows Vista's new user interface capabilities, you need to learn Microsoft's Windows Presentation Foundation (WPF). This new edition, fully updated for the official release of .NET 3.0, is designed to get you up to speed on this technology quickly. By page 2, you'll be writing a simple WPF application. By the end of Chapter 1, you'll have taken a complete tour of WPF and its major elements.

WPF is the new presentation framework for Windows Vista that also works with Windows XP. It's a cornucopia of new technologies, which includes a new graphics engine that supports 3-D graphics, animation, and more; an XML-based markup language, called XAML, for declaring the structure of your Windows UI; and a radical new model for controls.

This second edition includes new chapters on printing, XPS, 3-D, navigation, text and documents, along with a new appendix that covers Microsoft's new WPF/E platform for delivering richer UI through standard web browsers -- much like Adobe Flash. Content from the first edition has been significantly expanded and modified. Programming WPF includes:

  • Scores of C# and XAML examples that show you what it takes to get a WPF application up and running, from a simple "Hello, Avalon" program to a tic-tac-toe game

  • Insightful discussions of the powerful new programming styles that WPF brings to Windows development, especially its new model for controls

  • A color insert to better illustrate WPF support for 3-D, color, and other graphics effects

  • A tutorial on XAML, the new HTML-like markup language for declaring Windows UI

  • An explanation and comparison of the features that support interoperability with Windows Forms and other Windows legacy applications

  • WPF represents the best of the control-based Windows world and the content-based web world. Programming WPF helps you bring it all together.

    Table of Contents

    1. Programming WPF, 2nd Edition
    2. Dedication
    3. A Note Regarding Supplemental Files
    4. Forewords
      1. First Edition
      2. Second Edition
    5. Preface
      1. Who This Book Is For
      2. How This Book Is Organized
      3. What You Need to Use This Book
      4. Conventions Used in This Book
      5. Using Code Examples
      6. How to Contact Us
      7. Safari® Books Online
      8. Ian's Acknowledgments
      9. Chris's Acknowledgments
    6. 1. Hello, WPF
      1. WPF from Scratch
        1. Building Applications
        2. WPF Applications
        3. XAML
        4. Editing XAML
      2. XAML Browser Applications (XBAPs)
      3. Content Models
        1. XAML Property Element Syntax
      4. Layout
        1. Grid Layout
        2. XAML Attached Property Syntax
      5. Controls
      6. Data Binding
        1. XAML Markup Extensions
        2. Data Templates
      7. Dependency Properties
      8. Resources
        1. XAML Namespace Mapping Syntax
      9. Styles
      10. Animation
      11. Control Templates
      12. Graphics
      13. 3D
      14. Documents and Printing
      15. Where Are We?
    7. 2. Applications and Settings
      1. Application Lifetime
        1. Explicit Application Creation
        2. Application Access
        3. Implicit Application Creation
        4. Top-Level Windows
        5. Application Shutdown Modes
        6. Application Events
          1. Startup event
          2. Activated and Deactivated events
          3. DispatcherUnhandledException event
          4. SessionEnding event
          5. Exit event
        7. Application Instancing
          1. Single instance applications
        8. Other Application Services
      2. Application Deployment
        1. Simple Publishing
        2. The User Experience
        3. WPF ClickOnce Specifics
      3. Settings
        1. Designing Settings
        2. Using Settings
        3. Integrating Settings with WPF
      4. Where Are We?
    8. 3. Layout
      1. Layout Basics
      2. StackPanel
      3. WrapPanel
      4. DockPanel
      5. Grid
        1. Column Widths and Row Heights
        2. Spanning Multiple Rows and Columns
        3. Consistency Across Multiple Grids
        4. UniformGrid
      6. Canvas
      7. Viewbox
      8. Common Layout Properties
        1. Width and Height
        2. MinWidth, MaxWidth, MinHeight, and MaxHeight
        3. HorizontalAlignment and VerticalAlignment
        4. Margin
        5. Padding
        6. Visibility
        7. FlowDirection
        8. Panel.ZIndex
        9. RenderTransform and LayoutTransform
      9. When Content Doesn't Fit
      10. ScrollViewer
        1. Scrollable Region and IScrollInfo
      11. Custom Layout
      12. Where Are We?
    9. 4. Input
      1. Routed Events
        1. Halting Event Routing
        2. Determining the Target
        3. Routed Events and Normal Events
        4. Attached Events
      2. Mouse Input
        1. Mouse Input and Hit Testing
        2. Mouse State
      3. Keyboard Input
        1. Keyboard State
      4. Ink Input
      5. Commands
        1. Command Objects
          1. Defining commands
          2. Using commands in XAML
        2. Input Bindings
        3. Command Source
        4. Command Bindings
          1. Enabling and disabling commands
          2. Command routing
      6. Code-Based Input Handling Versus Triggers
      7. Where Are We?
    10. 5. Controls
      1. What Are Controls?
      2. Buttons
      3. Slider and Scroll Controls
      4. ProgressBar
      5. Text Controls
        1. Label
      6. ToolTip
      7. GroupBox and Expander
      8. List Controls
        1. List View
        2. Tree View
      9. Menus
      10. Toolbars
      11. GridSplitter
      12. Where Are We?
    11. 6. Simple Data Binding
      1. Without Data Binding
        1. Object Changes
        2. UI Changes
      2. Data Binding
        1. Bindings
        2. Implicit Data Source
        3. Data Islands
        4. Explicit Data Source
        5. Binding to Other Controls
        6. Value Conversion
        7. Editable Value Conversion
        8. Validation
          1. Custom validation rules
        9. Binding Path Syntax
        10. Relative Sources
        11. Update Source Trigger
      3. Debugging Data Binding
      4. Where Are We?
    12. 7. Binding to List Data
      1. Binding to List Data
        1. Current Item
          1. Getting the current item
          2. Navigating between items
        2. List Data Targets
        3. Display Members, Value Members, and Look-Up Bindings
        4. Data Templates
          1. Typed data templates
          2. DataTemplates and the DataContext
        5. List Changes
        6. Sorting
        7. Default Collection Views
        8. Filtering
        9. Grouping
        10. Declarative Sorting and Grouping
      2. Data Source Providers
        1. Object Data Provider
          1. Asynchronous data retrieval
          2. Passing parameters
        2. Binding to Relational Data
        3. XML Data Source Provider
          1. XML data islands
          2. XML binding without the data source provider
      3. Master-Detail Binding
      4. Hierarchical Binding
      5. Where Are We?
    13. 8. Styles
      1. Without Styles
      2. Inline Styles
      3. Named Styles
        1. The Target Type Attribute
        2. Reusing Styles
        3. Overriding Style Properties
        4. Extending Styles
        5. Setting Styles Programmatically
      4. Element-Typed Styles
        1. Element-Typed Styles and Derived Types
      5. Data Templates and Styles
        1. Data Templates Redux
        2. Data Templates with Style
      6. Triggers
        1. Property Triggers
        2. Multiple Triggers
        3. Multicondition Property Triggers
        4. Data Triggers
        5. Multicondition Data Triggers
        6. Event Triggers
      7. Where Are We?
    14. 9. Control Templates
      1. Beyond Styles
        1. Control Templates and Styles
        2. Template Binding
        3. Content Presenters
        4. Template Triggers
        5. Extending Templates
          1. Repurposing an existing property
          2. Defining a custom dependency property
        6. The Control Template Contract
          1. Property binding
          2. Named parts
          3. Content placeholders
          4. Placeholders indicated by properties
        7. Special-Purpose Elements
        8. Examining the Built-in Templates
      2. Logical and Visual Trees
      3. Data-Driven UI
      4. Where Are We?
    15. 10. Windows and Dialogs
      1. Window
        1. Window Look and Feel
        2. Window Lifetime
        3. Window Location and Size
        4. Window Owners
        5. Window Visibility and State
      2. Dialogs
        1. Common Dialogs
          1. Available common dialogs
        2. Custom Dialogs
          1. Dialog look and feel
          2. Dialog data exchange
          3. Handling OK and Cancel
          4. Data validation
          5. Modeless dialogs
      3. Where Are We?
    16. 11. Navigation
      1. NavigationWindow
      2. Pages
        1. Loose XAML
        2. Fragment Navigation
        3. Page Lifetime
        4. Passing Data Between Pages
        5. Page Functions
      3. Frames
      4. XBAPs
        1. XBAP Publication and Deployment
      5. Navigation to HTML
      6. Where Are We?
    17. 12. Resources
      1. Creating and Using Resources
        1. Resource Scope
          1. Defining custom system-scope resources
          2. Using system-scope resources
        2. Resource References
        3. Reusing Drawings
      2. Resources and Styles
        1. Skins and Themes
      3. Binary Resources
        1. Binary Resources and the Application Class
        2. Pack URIs
      4. Global Applications
        1. Building Localizable Applications with XAML
      5. Where Are We?
    18. 13. Graphics
      1. Graphics Fundamentals
        1. Integration
        2. Drawing Object Model
        3. Resolution Independence
          1. Scaling and rotation
          2. Resolution, coordinates, and "pixels"
        4. Shapes, Brushes, and Pens
        5. Composition
      2. Shapes
        1. Base Shape Class
        2. Rectangle
        3. Ellipse
        4. Line
        5. Polyline
        6. Polygon
        7. Path
          1. B├ęzier curves
          2. Combining shapes
          3. Path geometry text format
      3. Bitmaps
        1. Image
        2. ImageSource
        3. Creating Bitmaps
        4. Bitmap Encoders and Decoders
        5. Bitmap Effects
      4. Brushes and Pens
        1. Color
        2. SolidColorBrush
        3. LinearGradientBrush
        4. RadialGradientBrush
        5. ImageBrush, DrawingBrush, and VisualBrush
          1. TileBrush
          2. ImageBrush
          3. DrawingBrush
          4. VisualBrush
        6. Pen
      5. Transformations
      6. Visual Layer Programming
        1. Rendering On Demand
      7. Where Are We?
    19. 14. Text and Flow Documents
      1. Fonts and Text Styles
        1. Common Text Properties
        2. Fonts and Font Families
        3. FontSize
        4. Stretch
        5. Style
        6. Weight
        7. Decorations
        8. Text Trimming
        9. Text Wrapping and Hyphenation
        10. Text Alignment
      2. Text and the User Interface
        1. TextBlock
          1. Label and AccessText
        2. Flow Documents and Viewer Controls
        3. Visual Layer Text
          1. Glyph runs
          2. FormattedText
        4. Glyphs
      3. Text Object Model
        1. Inline
          1. Run
          2. Span
          3. LineBreak
          4. InlineUIContainer
        2. Block
          1. Paragraph
          2. List
          3. Table
          4. Section
          5. BlockUIContainer
        3. Figures and Floaters
        4. Images
        5. Text and Data Binding
        6. Coding with the Text Object Model
          1. TextPointer
      4. Typography
      5. Where Are We?
    20. 15. Printing and XPS
      1. XPS
        1. Open Packaging Conventions
      2. XPS Document Classes
        1. FixedDocumentSequence
        2. FixedDocument
        3. FixedPage
          1. Page sizing
          2. Page content limitations
          3. Fonts, bitmaps, and other resources
      3. Generating XPS Output
        1. Printing Fixed Documents
        2. Printing Visuals
        3. Printing with Document Paginators
        4. Asynchronous Printing
      4. XPS File Generation Features
        1. Package-Level XPS API
        2. Core Document Properties
        3. Thumbnails
        4. Hyperlinks
        5. Compression
      5. System.Printing
        1. PrintQueue
        2. PrintServer
        3. PrintSystemJobInfo
        4. PrintTicket and PrintCapabilities
        5. PrintDialog
        6. Media Description
      6. Displaying Fixed Documents
      7. Where Are We?
    21. 16. Animation and Media
      1. Animation Fundamentals
        1. Animation Types
        2. IAnimatable
        3. Triggers
      2. Timelines
        1. Hierarchy
        2. Duration
        3. Repetition
        4. Filling
        5. Speed
      3. Keyframe Animations
      4. Path Animations
      5. Clocks and Control
        1. Controlling Animations with Storyboards
        2. Controlling Animations with Clocks
      6. Transition Animations
      7. Audio and Video
        1. Simple Media Playback
        2. Using MediaElement in Clock Mode
          1. Slipping
      8. Where Are We?
    22. 17. 3D Graphics
      1. 3D Content in a 2D World
      2. Cameras
      3. Models
        1. Geometry3D
        2. Materials
          1. DiffuseMaterial
          2. SpecularMaterial
          3. EmissiveMaterial
          4. MaterialGroup
        3. ModelVisual3D
      4. Lights
        1. AmbientLight
        2. DirectionalLight
        3. PointLight
        4. SpotLight
      5. Textures
      6. Transforms
        1. TranslateTransform3D
        2. ScaleTransform3D
        3. RotateTransform3D
        4. Transform3DGroup
        5. MatrixTransform3D
      7. 3D Data Visualization
      8. Hit Testing
      9. Where Are We?
    23. 18. Custom Controls
      1. Custom Control Basics
      2. Choosing a Base Class
      3. Custom Functionality
        1. Properties
          1. Attached properties
          2. Value change notification
          3. Change notifications for property consumers
          4. Property metadata options
        2. Events
          1. Attached events
        3. Commands
      4. Supporting Templates in Custom Controls
        1. Property Binding
        2. Named Parts
        3. Content Placeholders
        4. Placeholders Indicated by Properties
      5. Default Styles
      6. UserControl
      7. Adorners
        1. AdornerDecorator
      8. Where Are We?
    24. A. XAML
      1. XAML Essentials
        1. Namespaces
        2. Generating Classes
        3. Properties
        4. Children
      2. Properties
        1. Property Element Syntax
        2. Attached Properties
          1. Attached properties and the property element syntax
      3. Markup Extensions
        1. Built-in Markup Extensions
          1. NullExtension
          2. TypeExtension
          3. ArrayExtension
          4. StaticExtension
          5. StaticResourceExtension
          6. DynamicResourceExtension
          7. ComponentResourceKey
          8. Binding
          9. RelativeSource
          10. TemplateBindingExtension
      4. Code Behind
        1. Code in XAML
      5. Loading XAML
        1. Parsing XAML at Runtime
        2. Loading Compiled XAML (BAML)
    25. B. Interoperability
      1. WPF and HWNDs
      2. Hosting a Windows Form Control in WPF
      3. Hosting a WPF Control in Windows Forms
      4. Hosting WPF in Native HWND Apps
      5. WPF and ActiveX Controls
      6. WPF and HTML
      7. Limitations of WPF/HWND Interop
        1. Airspace
        2. Transforms, Events, and Nested Interop
    26. C. Asynchronous and Multithreaded WPF Programming
      1. The WPF Threading Model
        1. DispatcherObject
      2. The Dispatcher
        1. Obtaining a Dispatcher
        2. Getting Onto the Right Thread with a Dispatcher
        3. DispatcherOperation
        4. DispatcherTimer
        5. Multiple UI Threads and Dispatchers
      3. The Event-Based Asynchronous Pattern
        1. BackgroundWorker
    27. D. WPF Base Types
      1. DispatcherObject
      2. DependencyObject
      3. Visual
      4. Visual3D
      5. UIElement
      6. FrameworkElement
      7. Decorator
      8. Panel
      9. Shape
      10. Control
      11. ContentControl
      12. HeaderedContentControl
      13. UserControl
      14. ItemsControl
      15. HeaderedItemsControl
      16. Selector
      17. ContentElement
      18. FrameworkContentElement
      19. Freezable
      20. Animatable
    28. E. Silverlight
      1. Why Silverlight?
      2. What Is Silverlight?
        1. Hello, Silverlight
      3. Silverlight XAML
        1. Layout Model
        2. Namespaces
        3. Graphics
        4. Mouse Cursors
        5. Measuring Text
        6. Transformations
        7. Animations
      4. Silverlight and WPF
      5. Development Model
        1. Hosting in HTML
        2. End-User Installation
        3. Handling XAML Errors
        4. Event Model
        5. Working with XAML Properties
        6. The Plug-in
        7. Working with the XAML Object Model
        8. An Example: Creating a Button
        9. Creating Dynamic XAML
        10. Controlling Media
        11. Controlling Animations
          1. Controlling animation execution
          2. Delaying storyboards
        12. Mixing Silverlight and HTML
        13. The Silverlight Downloader
      6. ASP.NET and Silverlight
        1. Commingling with ASP.NET
        2. Dynamic XAML
        3. XAML and User Controls
      7. A Taste of Silverlight 1.1
      8. Tool Support
        1. Expression Toolset
          1. Expression Design
          2. Expression Blend
          3. Expression Media
        2. Visual Studio
        3. Other Tools
      9. Examples in the World
      10. Where Are We?
    29. F. Color Inserts
    30. Index
    31. About the Authors
    32. Colophon
    33. Copyright