You are previewing Sams Teach Yourself WPF in 24 Hours.
O'Reilly logo
Sams Teach Yourself WPF in 24 Hours

Book Description

Printed entirely in color, with helpful figures and syntax coloring to make code samples appear as they do in Visual Studio.

In just 24 sessions of one hour or less, you will be able to begin effectively using WPF to solve real-world problems, developing rich user interfaces in less time than you thought possible.

Using a straightforward, step-by-step approach, each lesson builds on a real-world foundation forged in both technology and business matters, allowing you to learn the essentials of WPF from the ground up.

Step-by-step instructions carefully walk you through the most common questions, issues, and tasks.

The Q&A sections, quizzes, and exercises help you build and test your knowledge.

By the Way notes present interesting pieces of information.

Did You Know? tips offer advice or teach an easier way to do something.

Watch Out! cautions advise you about potential problems and help you steer clear of disaster.

Learn how to...

  • Use XAML to build user interfaces

  • Leverage data binding to minimize tedious code

  • Create visually engaging applications

  • Architect and design WPF applications using proven patterns such as MVP

  • Incorporate audio and video into your applications

  • Customize controls with styles, templates, and animation

  • Apply best practices for developing software with WPF

  • Deploy WPF applications to the desktop and Web

  • Take advantage of WPF’s advanced printing capabilities

  • Grow as a developer by improving your overall software design skills

  • Introduction 1

    Part I                   Getting Started

    1      What WPF Is and Isn’t 5

    2      Understanding XAML 17

    3      Introducing the Font Viewer 27

    4      Handling Application Layout 41

    5      Using Basic Controls 59

    6      Introducing Data Binding 75

    Part II        Reaching the User

    7      Designing an Application 93

    8      Building a Text Document Editor 107

    9      Getting a Handle on Events 121

    10    Commands 145

    11    Output 157

    Part III      Visualizing Data

    12    Building a Contact Manager 177

    13    Presenters and Views 193

    14    Resources and Styles 211

    15    Digging Deeper into Data Binding 229

    16    Visualizing Lists 251

    Part IV       Creating Rich Experiences

    17    Building a Media Viewer 267

    18    Drawing with Shapes 291

    19    Colors and Brushes 315

    20    Transforms and Effects 331

    21    Using Control Templates 347

    22    Triggers 369

    23    Animation 383

    24    Best Practices 407

    Part V         Appendixes

    Appendix A: Tools and Resources 423

    Appendix B: 3D Tutorial Using ZAM 3D 427

    Appendix C: Project Source (downloadable) 437

    Index 439

    Table of Contents

    1. Copyright
      1. Dedication
    2. About the Authors
    3. Acknowledgments
    4. We Want to Hear from You!
    5. Reader Services
    6. Introduction
      1. Audience and Organization
      2. Farther Up and Further In
    7. I. Getting Started
      1. 1. What WPF Is and Isn’t
        1. What Is WPF?
        2. Getting to Know the Features of WPF
          1. Declarative UI
          2. Intelligent Layout
          3. Scalable Graphics
          4. Templates
          5. Binding
          6. Styling
          7. Triggers
          8. Animation
          9. 3D
        3. Why Use WPF?
        4. Comparing WPF to Other Options
        5. The Pieces of .NET Framework
        6. Tools for WPF
        7. Constrasting WPF with Silverlight
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
      2. 2. Understanding XAML
        1. What Is XAML?
        2. The Syntax of XAML
          1. Setting Properties That Aren’t Simple Types
          2. The Content Property
          3. Markup Extensions
        3. Summary
        4. Q&A
        5. Workshop
          1. Quiz
          2. Answers
      3. 3. Introducing the Font Viewer
        1. Building the Font Viewer
          1. Setting Up a New Project in Visual Studio 2008
          2. Basic Project Files
          3. What Are Code-Behind Files?
          4. Separating Your Concerns
          5. Renaming a XAML File
          6. Developing the Application
          7. Polishing a Few Things
        2. Summary
        3. Q&A
        4. Workshop
          1. Quiz
          2. Answers
      4. 4. Handling Application Layout
        1. Understanding Layout
          1. Layout Panels and Decorators
          2. FrameworkElement
        2. Using Alignment and Margin
        3. Using Panels to Create Dynamic Layouts
          1. Keeping It Simple with StackPanel
        4. Using a StackPanel
          1. Organize Your UI with DockPanel
            1. Attached Properties
        5. Mastering DockPanel
          1. Leveraging the Grid for Ultimate Layout Control
        6. Using a Grid
          1. GridSplitter
        7. Understanding How WrapPanel Works
        8. Exploit Canvas for Precision Layout
        9. Enhancing a UI with Decorators
        10. Building Up a Layout
        11. Summary
        12. Q&A
        13. Workshop
          1. Quiz
          2. Answers
          3. Activities
      5. 5. Using Basic Controls
        1. Leveraging Controls
        2. Working with Text
        3. Displaying Rich Text with the TextBlock
        4. Gathering Text from the User
        5. Accessing Controls with a Label
        6. Using Buttons
        7. Triggering Actions with a Button
        8. Making Choices with ToggleButtons
        9. Displaying a List with ListBox
        10. Summary
        11. Q&A
        12. Workshop
          1. Quiz
          2. Answers
          3. Activities
      6. 6. Introducing Data Binding
        1. What Is Data Binding?
          1. Understanding the Data Binding Syntax
        2. Binding Two Controls Together
        3. Two-Way Data Binding
          1. Binding to the Collection of Fonts
          2. Introducing DataContext
          3. What Makes Data Binding Work?
        4. Demonstrating Automatic Change Notification
        5. Another Data Binding Syntax
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
    8. II. Reaching the User
      1. 7. Designing an Application
        1. Deployment Models in WPF
          1. Converting the Font Viewer to XAML Only
          2. Converting the Font Viewer to an XBAP
          3. Publishing an XBAP
        2. Navigation Models in WPF
        3. User Controls
        4. Summary
        5. Q&A
        6. Workshop
          1. Quiz
          2. Answers
      2. 8. Building a Text Document Editor
        1. Designing a Text Editor
        2. Creating the Application Layout
        3. Adding Usability with ToolBars
        4. Increasing Maintainability with User Controls
        5. Using a Menu
        6. Working with RichTextBox
        7. Summary
        8. Q&A
        9. Workshop
          1. Quiz
          2. Answers
          3. Activities
      3. 9. Getting a Handle on Events
        1. What Are Routed Events?
          1. Understanding RoutedEventArgs
        2. Using Routed Events
        3. Handling Events in the Text Editor
        4. Making the Text Editor Work as Expected
        5. Preview Events
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
      4. 10. Commands
        1. What Are Commands?
        2. Using Commands
        3. Binding Commands to the Menu
        4. Customizing Commands
        5. Adding More Commands to the Text Editor
        6. Determining If a Command Is Available
        7. Creating an Input Binding
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
          3. Activities
      5. 11. Output
        1. Making the Text Editor Print
        2. Implementing a Custom Document Paginator
        3. Developing a Print Manager
        4. Adding Print Preview
        5. Outputting Bitmaps
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
    9. III. Visualizing Data
      1. 12. Building a Contact Manager
        1. Design a Contact Manager
        2. Choosing an Architecture
        3. Creating the Solution and Application Shell
          1. Expander
          2. Slider
          3. TabControl
        4. Defining the Model
        5. Abstract the Data Store
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. 13. Presenters and Views
        1. Creating a Custom Base Class for Presenters
        2. Completing the Tab UI Infrastructure
        3. Implementing the Application Presenter
        4. Enhancing the Shell
          1. ScrollViewer
          2. ItemsControl
        5. Building an Edit Contact Screen
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      3. 14. Resources and Styles
        1. Defining Resources
          1. Color
        2. Combining Resources
          1. Brushes
        3. Using Resources in the UI
        4. Factoring Resource Files
          1. StaticResource Versus DynamicResource
        5. Defining Styles
        6. Using Keyed Styles
        7. Factoring Styles
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
          3. Activities
      4. 15. Digging Deeper into Data Binding
        1. Handling Advanced Data Binding Scenarios
          1. Adding the Contact List
        2. Binding Data in Code
        3. Observing Change in Collections
          1. Demonstrating an Observable Collection
          2. Displaying the List View
          3. Data Templates
        4. Formatting Bound Data
          1. Hooking Up a Converter
          2. Converting Back
          3. Parameterizing Converters
        5. Understanding Collection Views
          1. Collection Views and XAML
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
      5. 16. Visualizing Lists
        1. The Control Hierarchy
        2. Dissecting ItemsControl
          1. Items
          2. Rendering
            1. ItemContainerGenerator
            2. DataTemplateSelector
            3. StyleSelector
          3. Layout
        3. Customizing the SideBar
        4. Studying Selector
        5. Using Selector
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
    10. IV. Creating Rich Experiences
      1. 17. Building a Media Viewer
        1. Defining the Requirements
        2. Setting Up the Solution
        3. Implementing the Menu Screen
        4. Building the Model
        5. Creating the Picture Screen
        6. Understanding Media
        7. Finalizing the Media Player Functionality
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
          3. Activities
      2. 18. Drawing with Shapes
        1. Drawing Basic Shapes
          1. Lines and Strokes
          2. Simple Shapes and Fills
          3. Ellipses and Rectangles
          4. A Header for the Media Viewer
        2. Styling the Media Controls
        3. Paths and Complex Drawings
        4. Stream Geometry
        5. Summary
        6. Q&A
        7. Workshop
          1. Quiz
          2. Answers
          3. Activities
      3. 19. Colors and Brushes
        1. Using Colors
        2. Choosing Colors
        3. Introducing Brushes
          1. SolidColorBrush
        4. Leveraging LinearGradientBrush
        5. Working with RadialGradientBrush
        6. Understanding DrawingBrush
        7. Applying a VisualBrush
        8. Summary
        9. Q&A
        10. Workshop
          1. Quiz
          2. Answers
          3. Activities
      4. 20. Transforms and Effects
        1. Understanding Transforms
          1. TranslateTransform
          2. SkewTransform
          3. RotateTransform
          4. ScaleTransform
          5. TransformGroup
        2. Differentiating Render and Layout Transforms
        3. Adding Transforms to the Media Viewer
        4. Introducing BitmapEffect
          1. DropShadowBitmapEffect
          2. OuterGlowBitmapEffect
          3. BlurBitmapEffect
          4. EmbossBitmapEffect
          5. BevelBitmapEffect
          6. BitmapEffectGroup
        5. Adding a BitmapEffect to the Media Viewer
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      5. 21. Using Control Templates
        1. Understanding Control Templates
          1. Placing Content Inside the Template
        2. Telling the Template What to Do
          1. Choosing Panels in Items Controls
        3. Adding Templates to Our Application
        4. Identifying Special Parts in Templates
          1. Creating a Template for a Slider
        5. Creating a Flexible Template
        6. The Benefit of Control Templates
        7. Summary
        8. Q&A
        9. Workshop
          1. Quiz
          2. Answers
          3. Activities
      6. 22. Triggers
        1. Leveraging Triggers
        2. Using a DataTrigger
        3. Adding Animation with EventTrigger
        4. Understanding Trigger Restrictions
        5. Advanced Trigger Scenarios
        6. Summary
        7. Q&A
        8. Workshop
          1. Quiz
          2. Answers
          3. Activities
      7. 23. Animation
        1. Understanding Animation in General
        2. Understanding Animation in WPF
          1. Making an Animation Tick
        3. Animations in the Media Viewer
        4. Resolving the Target Property
        5. Where to Put Your Animations
        6. Achieving the Desired Effect
          1. Acceleration and Deceleration
          2. What Happens When an Animation Ends?
          3. Making an Animation Repeat
        7. Animating with Key Frames
        8. Animating with a Path
        9. Controlling an Animation
        10. Alternative Animation Techniques
          1. Local Animations
        11. Clock Animations
          1. Manual Animation
        12. Applying Animations to Media Viewer
        13. Summary
        14. Q&A
        15. Workshop
          1. Quiz
          2. Answers
          3. Activities
      8. 24. Best Practices
        1. Application Design
          1. Orthogonality
          2. Single Responsibility Principle/Separation of Concerns
          3. Don’t Repeat Yourself
          4. Patterns
          5. Inversion of Control/Dependency Injection
          6. Ya Aren’t Gonna Need It
          7. WPF as an Architecture Example
        2. UI Design and Architecture Concerns
          1. Organization
          2. Model View Presenter (MVP)
          3. Usability
        3. Technical Considerations
          1. UI Layout
          2. Resources
          3. Styles
          4. Templates
        4. Coding Conventions
          1. Code
          2. XAML
        5. Summary
        6. Q&A
        7. Workshop
          1. Quiz
          2. Answers
          3. Activities
    11. V. Appendixes
      1. A. Tools and Resources
        1. Developer Tools
          1. Visual Studio 2008
          2. Kaxaml
          3. Mole
        2. Designer Tools
          1. Expression Blend
          2. Mobiform’s Aurora
          3. ZAM 3D
        3. XAML Converters
          1. 2D
          2. 3D
          3. Other Formats
        4. Application Frameworks
          1. Caliburn
          2. WPF CAB
          3. Prism
        5. Additional Resources
          1. Icons
      2. B. 3D Tutorial Using ZAM 3D
        1. Understanding the Interface
        2. Creating a Carrot
      3. C. Project Source (downloadable)