You are previewing Windows Presentation Foundation 4.5 Cookbook.
O'Reilly logo
Windows Presentation Foundation 4.5 Cookbook

Book Description

For C# developers, this book offers a fast route to getting more closely acquainted with the ins and outs of Windows Presentation Foundation. The recipe approach smoothes out the complexities and enhances learning.

  • Full of illustrations, diagrams, and tips with clear step-by-step instructions and real world examples

  • Gain a strong foundation of WPF features and patterns

  • Leverage the MVVM pattern to build decoupled, maintainable apps

  • In Detail

    Windows Presentation Foundation (WPF) provides developers with a unified programming model for building rich Windows smart client user experiences that incorporate UI, media, and documents.

    WPF has become the leading technology for developing rich client applications on the Windows platform, packed with features and capabilities. However, WPF is big; in fact, it’s huge, causing a steep learning curve for the beginner and even for those already using some WPF features.

    Windows Presentation Foundation 4.5 Cookbook provides clear recipes for common WPF tasks. It includes detailed explanations and code examples for customizing and enhancing the basic scenarios, while gaining a deep understanding of WPF mechanics and capabilities.

    WPF is different and requires a different mind-set and approach. This book provides recipes and insights not only in its design but also its practical implementation details.

    Starting from the foundations of WPF, such as dependency properties and XAML, the book touches on all major WPF aspects, such as controls and layout, resources, and digs deep into its unprecedented data binding capabilities.

    The book shows data and control templates in action, which allow full customizations of displayed data and controls in a declarative way. Supported by styles and resources makes data binding all the more powerful. The Model View View-Model pattern is presented as an effective way of maximizing decoupling of components, while providing an elegant way of expanding applications while maintaining a tight grip on complexity.

    The later parts discuss custom elements and controls – the ultimate customization mechanism, and looks at multithreading issues, and how .NET 4.5 task parallelism features can enhance application performance.

    Table of Contents

    1. Windows Presentation Foundation 4.5 Cookbook
      1. Windows Presentation Foundation 4.5 Cookbook
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
        2. Why Subscribe?
        3. Free Access for Packt account holders
        4. Instant Updates on New Packt Books
      6. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      7. 1. Foundations
        1. Introduction
          1. XAML
          2. XAML and compilation
          3. Dependency properties
        2. Creating custom type instances in XAML
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
          3. Creating a dependency property
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Property value inheritance
              2. Why "dependency"?
              3. Dependency property levels
          4. Using an attached property
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Why an attached property?
              2. Does the declaring type "own" the property?
            5. See also
          5. Creating an attached property
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Reusing existing attached properties
            5. See also
          6. Accessing a static property from XAML
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
          7. Creating a custom markup extension
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Don't go overboard
          8. Handling routed events
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Stopping bubbling or tunneling
              2. Attached events
      8. 2. Resources
        1. Introduction
        2. Using logical resources
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Adding or deleting resources dynamically
            2. Modifying resources
            3. Resources that use other resources
            4. Non-shared resources
            5. Other locations for resources
        3. Dynamically binding to a logical resource
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Using user-selected colors and fonts
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Using binary resources
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Embedded Resource
        6. Accessing binary resources in code
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Accessing binary resources from another assembly
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Managing logical resources
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Duplicated keys
      9. 3. Layout and Panels
        1. Introduction
          1. The layout process
          2. Coordinates systems in WPF
        2. Creating a table-like user interface
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Shared row/column size
            2. Placement in the same cell
            3. The power of the Grid
            4. Adding rows/columns dynamically
            5. The UniformGrid
        3. Dynamically sizing grid rows/columns
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Creating a scrollable user interface
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Creating a border around panels and elements
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Placing elements in exact positions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Canvas has no background
            2. Canvas is not limited to its bounds
        7. Adding/removing elements to a panel dynamically
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Creating a tabbed user interface
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Implementing drag-and-drop
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Built-in drag-and-drop
            2. Drag-and-drop to other applications
      10. 4. Using Standard Controls
        1. Introduction
        2. Working with text
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Using content controls
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Headered content controls
          5. See also
        4. Displaying images
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating tooltips
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Deeper tooltip customization
            2. Realistic tooltips
        6. Creating a list of items
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Creating a standard menu
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Other MenuItem properties and events
        8. Creating a context menu
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Selecting options with checkboxes and radio buttons
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Manipulating tab order and focus
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Keyboard focus versus logical focus
      11. 5. Application and Windows
        1. Introduction
        2. Creating a window
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Selecting the startup window dynamically
            2. Accessing command line arguments
        3. Creating a dialog box
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Modeless dialogs
        4. Using the common dialog boxes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. What about colors and fonts?
            2. The Windows API Code Pack
        5. Creating ownership between windows
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Creating a custom shaped window
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. What about reusability?
        7. Creating a single instance application
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Handling an unhandled exception
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      12. 6. Data Binding
        1. Introduction
        2. Element to element binding
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Binding mode
            2. Update source trigger
            3. Updating the source or target manually
        3. Binding to a single object
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. When bindings fail
            2. Other ways of getting a source binding object
            3. Implementing INotifyPropertyChanged
            4. Implementing SetProperty with Visual Studio 2012 and C# 5.0
        4. Binding to a collection
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Synchronizing selected items
            2. Data binding and the Items property are mutually exclusive
            3. What about "real" data?
        5. Using data templates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Data type based data templates
            2. Data template selectors
        6. Using value converters
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using converters for debugging
            2. Formatting strings
            3. Customizing with data triggers
        7. Creating a master-detail view
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. A simpler selected item binding
        8. Sorting and filtering bound collections
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. More features of ICollectionView
            2. Live shaping
        9. Grouping bound collections
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Grouping by a non-property
          5. See also
        10. Binding to multiple properties
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Binding hierarchical data to a TreeView
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        12. Presenting data in a grid
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Editing with a template-based column
            2. Selecting, resizing, and sorting
            3. Other customization options
        13. Validating data
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
              1. Custom validation rules
              2. Custom error template
              3. Using data annotations
      13. 7. Commands and MVVM
        1. Introduction
        2. Using routed commands
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Built-in implementations
            2. Command sources
            3. Alternative ICommand implementations
        3. Implementing a basic MVVM application
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Implementing ICommand
            2. Blendability
        4. Building a simple MVVM framework
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Non-ICommandSource elements and other events
            2. What about Prism?
        5. Building a complete MVVM style application
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. MVVM implementations
        6. Creating an undo/redo system
          1. Getting ready
          2. How to do it...
          3. How it works...
      14. 8. Styles, Triggers, and Control Templates
        1. Introduction
        2. Creating and using styles
          1. Getting ready
          2. How to do it...
            1. How it works...
            2. There's more...
              1. Style inheritance
              2. Other places to set styles
          3. Applying a style automatically
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
          4. Creating a property trigger
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
              1. Trigger limitations
              2. When to use triggers
              3. Other locations of triggers
              4. Trigger priorities
            5. See also
          5. Using data triggers
            1. Getting ready
            2. How to do it...
              1. How it works...
            3. Creating an event trigger
              1. Getting ready
              2. How to do it...
              3. How it works...
              4. There's more...
            4. Creating a multi trigger
              1. Getting ready
              2. How to do it...
              3. How it works...
              4. There's more...
            5. Using behaviors
              1. Getting ready
              2. How to do it...
              3. How it works...
              4. There's more...
                1. Custom behaviors
            6. Replacing the control template of a progress bar
              1. Getting ready
              2. How to do it...
              3. How it works...
              4. There's more...
                1. What about the control's properties?
                2. Combining a control template with a style
                3. Extending a template with attached properties
                4. Can we replace just part of a template?
                5. What about the Visual State Manager?
            7. Replacing the control template of a scroll bar
              1. Getting ready
              2. How to do it...
              3. How it works...
            8. Customizing selection in a Selector control
              1. Getting ready
              2. How to do it...
              3. How it works...
              4. There's more...
      15. 9. Graphics and Animation
        1. Introduction
        2. Creating a custom shape
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Geometries
            2. Built-in shapes
            3. Shapes versus geometries
        3. Applying transforms on elements
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Other uses for transforms
        4. Manipulating a bitmap programmatically
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. How about higher-level access to WriteableBitmap?
          5. See also
        5. Creating adorners
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Creating property-based animations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Alternative way to specify the animation property
            2. More on storyboards
            3. Animations with Expression Blend
            4. Should I always use animations?
        7. Creating path-based animations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Animation performance
        8. Creating custom animations
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Adding animation easing to animations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Using custom effects with pixel shaders
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using the built-in effects
            2. Other shader types
            3. What about the BitmapEffect class and its derivatives?
      16. 10. Custom Elements
        1. Introduction
        2. Creating a user control
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Optimizing converters
            2. Adding a tunneling event
        3. Handling standard commands in a user control
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Creating a custom (templated) control
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Other things to set in code
            2. What is that Generic.xaml?
            3. Refactoring of Generic.xaml
        5. Customizing a default template of custom control
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Creating a custom panel
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Where are custom panels used?
        7. Creating a lightweight custom element
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Dependency property ownership
            2. More DrawingContext
      17. 11. Threading
        1. Introduction
        2. Updating the UI from a non-UI thread
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Dispatcher alternative
            2. Dispatcher enhancements in WPF 4.5
            3. More Dispatcher
            4. What about data binding?
        3. Adding cancelation support
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Never cancel by aborting a thread
        4. Using the BackgroundWorker component
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Did we really have to pass the argument to RunWorkerAsync?
            2. What about the Task Parallel Library?
        5. Adding cancellation and progress with BackgroundWorker
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. What about Parallel.For?
        6. Using a timer to do periodic updates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Using C# 5.0 to perform asynchronous operations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. What about non-CPU bound operations?
            2. More async