Module 12: Attached Properties and Behaviors in WPF

Download Report

Transcript Module 12: Attached Properties and Behaviors in WPF

Module 12 Attached Properties and Behaviors in WPF

Module Overview • Implementing Attached Properties • Implementing Expression Blend Behaviors, Triggers, and Actions • Implementing Drag-and-Drop User Interfaces

Lesson 1: Implementing Attached Properties • Understanding Attached Properties • Using Attached Properties • Implementing Attached Properties • Implementing Attached Behavior by Using Attached Properties

Understanding Attached Properties Similar to dependency properties with the following differences: • You do not define a property wrapper • You do not need to derive from the DependencyObject class • The value is stored with the attached element, not the declaring class instance Common WPF attached properties: • • • Grid.Column, Grid.ColumnSpan, Grid.Row, and Grid.RowSpan

DockPanel.Dock

Canvas.Left, Canvas.Top, and Canvas.ZIndex

Using Attached Properties Declaring type typically follows one of three models: • Acts as parent element; child elements set the attached property values • Could be the child element for different parent elements or content models • Provides a service Using attached properties in XAML: DockPanel.Dock="Top" >Hello World Using attached properties in code: CheckBox myCheckBox = new CheckBox(); myCheckBox.Content = "Hello World"; DockPanel.SetDock(myCheckBox, Dock.Top);

Implementing Attached Properties To implement an attached property: • Declare the property by using DependencyProperty.RegisterAttached • Implement the Get accessor • Implement the Set accessor public static readonly DependencyProperty IsCustomSourceProperty = DependencyProperty.RegisterAttached( "IsCustomSource", typeof(bool), typeof(MyClass)); public static bool GetIsCustomSource(UIElement target) { return (bool)target.GetValue(IsCustomSourceProperty); } public static void SetIsCustomSource(UIElement target, bool value) { target.SetValue(IsCustomSourceProperty, value); }

Implementing Attached Behavior by Using Attached Properties Using attached properties to provide a service: • Referred to as attached behavior • Examples include: • • • Drag-and-drop operations Panning and zooming Changing element position • Input validation public static readonly DependencyProperty IsDragSourceProperty = DependencyProperty.RegisterAttached( "IsDragSource", typeof(bool), typeof(MouseMoveAttachedBehavior), new FrameworkPropertyMetadata(true, OnIsDragSourceChanged)); • Hook events on the target element by using FrameworkPropertyMetadata private static void OnIsDragSourceChanged( DependencyObject source, DependencyPropertyChangedEventArgs e) { UIElement dragSource = (UIElement)source; dragSource.MouseLeftButtonDown += (s, e) => { // Implement MouseLeftButtonDown handling here.

} }

Lesson 2: Implementing Expression Blend Behaviors, Triggers, and Actions • Understanding Expression Blend Behaviors • Implementing Expression Blend Behaviors • Understanding Expression Blend Triggers and Actions • Implementing Expression Blend Triggers and Actions

Understanding Expression Blend Behaviors Expression Blend behaviors: • Formalize attached behaviors • Provide reusable packaged code • Can be added by dragging items from the Assets panel • Implemented by using the Behavior class • Can be downloaded from the Expression Gallery http://gallery.expression.microsoft.com/ Using Expression Blend behaviors in XAML:

Implementing Expression Blend Behaviors using System.Windows; using System.Windows.Interactivity; Constraint type public class MyButtonBehavior : Behavior