Module 12 Attached Properties and Behaviors in WPF.

Slides:



Advertisements
Similar presentations
Module 1: Creating an Application by Using Windows Presentation Foundation Overview of WPF Creating a Simple WPF Application Handling Events and Commands.
Advertisements

Module 1: Creating Responsive Pages with Ajax Creating Partial-Page Updates by Using AJAX Scripting Actions on the Web Client.
Master Pages, User Controls, Site Maps, Localization Svetlin Nakov Telerik Corporation
Module 10 WPF 2-D Graphics, Multimedia, and Printing.
Dinko Jakovljević Microsoft Student Partner | BambooLab
Managing User Settings with Group Policy
An Introduction To Silverlight Gergely Orosz
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Syncing Audio, Video and Animations in Silverlight Dan Wahlin.
Microsoft ® Official Course Monitoring and Troubleshooting Custom SharePoint Solutions SharePoint Practice Microsoft SharePoint 2013.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
.NET Database Technologies: Introduction to WPF and Entity Framework DataBinding.
Module 7: Object-Oriented Programming in Visual Basic .NET
Module 11 Control Customization. Module Overview Overview of Control Authoring Creating Controls Managing Control Appearance by Using Visual States Integrating.
Windows Presentation Foundation. Agenda Introduction Developing Applications WPF and WF interoperability Custom Controls Styles and Templates Data Binding.
Virtual techdays INDIA │ Nov 2010 Developing Office Biz Application using WPF on Windows 7 Sarang Datye │ Sr. Consultant, Microsoft Sridhar Poduri.
INSPIRING CREATIVE AND INNOVATIVE MINDS Module 4: Adding Code to a Microsoft ASP.NET Web Form Implementing Code-Behind Pages Adding Event Procedures to.
Introduction to Matlab & Data Analysis
C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,
Windows Presentation Foundation Adam Calderon Principal Engineer Interknowlogy LLC
UNIVERSITY of GREENWICH 1E. I. Teodorescu Dependency/Attached properties Enumerations Starting a game – flying pig Visual Application Development E. I.
Basic WPF Controls Doncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer
Module 6: Configuring User Environments Using Group Policy.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
Object Oriented Software Development 9. Creating Graphical User Interfaces.
Module 9 Configuring Messaging Policy and Compliance.
Web Games Programming Unity Scripting Fundamentals.
Module 10 Administering and Configuring SharePoint Search.
Module 2 Introduction to Visual Studio 2010 and WPF Version 4.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Controls. Adding Controls to Form -You can pick controls from the toolbox. -To add the controls from Toolbox to the Form You have be in design view. -To.
Module 7 Data Binding to Collections. Module Overview Binding to Collections of Objects Using Collection Views Create Master-Detail User Interfaces Using.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Windows Presentation Foundation (WPF). Introduction Separates appearance of user interface from behavior Appearance usually specified by XAML Behavior.
Rujchai Ung-arunyawee Department of Computer Engineering Khon Kaen University.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Module 13 Animations in WPF. Module Overview Using Animations Using Triggers Implementing Data Visualizations.
Object Oriented Programming.  Interface  Event Handling.
Module 14 Application Settings, State, and Life Cycle.
6 ListViewGridViewFlipView.
Module 3 Designing and Developing a User Interface.
Module 8 Enhancing User Interface Responsiveness.
Module 4: Creating a Web Application with Web Forms
Module 4 Taking Control of the User Interface. Module Overview Sharing Logical Resources in an Application Creating Consistent User Interfaces by Using.
Understanding Desktop Applications Lesson 5. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understanding Windows Forms Applications Understand.
CPSC 481 – Week #7 Sowmya Somanath
Understanding Desktop Applications Lesson 5. Understanding Windows Forms Applications Windows Forms applications are smart client applications consisting.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe.
Soyatec Contents Needs Architecture XAML fundamentals Data Binding Advanced features Style Q&A.
© Copyright SELA software & Education Labs Ltd Baruch Hirsch St.Bnei Brak Israel
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Java FX: Scene Builder.
Leading edge windows development
Important New Concepts In WPF
Ben Riga 02 | Basics of View Models Ben Riga
Delegates and Events 14: Delegates and Events
Introduction to Triggers
Build Windows 10 UWP MVVM Apps with Prism
Jim Fawcett CSE775 – Distributed Objects Spring 2011
1/10/2019 JavaFX Events COSC 330.
DEV312 基于WPF的数据绑定.
.NET and .NET Core 8. WPF Hierarchy Pan Wuming 2017.
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
CIS 199 Final Review.
Brown Bag Seminar Summer 2007
Presentation transcript:

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 Common WPF attached properties: Grid.Column, Grid.ColumnSpan, Grid.Row, and Grid.RowSpan DockPanel.Dock Canvas.Left, Canvas.Top, and Canvas.ZIndex Similar to dependency properties with the following differences: 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

Using attached properties in XAML: Using Attached Properties Hello World Hello World 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 code: CheckBox myCheckBox = new CheckBox(); myCheckBox.Content = "Hello World"; DockPanel.SetDock(myCheckBox, Dock.Top); CheckBox myCheckBox = new CheckBox(); myCheckBox.Content = "Hello World"; DockPanel.SetDock(myCheckBox, Dock.Top);

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); } 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 Properties To implement an attached property: Declare the property by using DependencyProperty.RegisterAttached Implement the Get accessor Implement the Set accessor

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 Hook events on the target element by using FrameworkPropertyMetadata public static readonly DependencyProperty IsDragSourceProperty = DependencyProperty.RegisterAttached( "IsDragSource", typeof(bool), typeof(MouseMoveAttachedBehavior), new FrameworkPropertyMetadata(true, OnIsDragSourceChanged)); public static readonly DependencyProperty IsDragSourceProperty = DependencyProperty.RegisterAttached( "IsDragSource", typeof(bool), typeof(MouseMoveAttachedBehavior), new FrameworkPropertyMetadata(true, OnIsDragSourceChanged)); private static void OnIsDragSourceChanged( DependencyObject source, DependencyPropertyChangedEventArgs e) { UIElement dragSource = (UIElement)source; dragSource.MouseLeftButtonDown += (s, e) => { // Implement MouseLeftButtonDown handling here. } 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

Using Expression Blend behaviors in XAML: Understanding Expression Blend Behaviors <il:MouseDragElementBehavior ConstrainToParentBounds="True" /> <il:MouseDragElementBehavior ConstrainToParentBounds="True" /> 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

Implementing Expression Blend Behaviors using System.Windows; using System.Windows.Interactivity; public class MyButtonBehavior : Behavior { public MyButtonBehavior() : base() { } protected override OnAttached() { this.AssociatedObject.Click += this.Behavior_Click; } protected override OnDetaching() { this.AssociatedObject.Click – this.Behavior_Click; } private void Behavior_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello World"); } using System.Windows; using System.Windows.Interactivity; public class MyButtonBehavior : Behavior { public MyButtonBehavior() : base() { } protected override OnAttached() { this.AssociatedObject.Click += this.Behavior_Click; } protected override OnDetaching() { this.AssociatedObject.Click – this.Behavior_Click; } private void Behavior_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello World"); } Hook an event on the associated object Unhook the event from the associated object Behavior implementation Constraint type The constraint type must derive from DependencyObject

Triggers: Understanding Expression Blend Triggers and Actions Source Element Trigger Target Action Actions: An object that you invoke to perform an action Implemented by using the TriggerAction class Change a property Call a method Open a window Navigate to a page Set focus Contain one or more actions Implemented by using the TriggerBase class EventTrigger DataTrigger

Implementing Expression Blend Triggers and Actions Implementing triggers: Derive from the TriggerBase class Override the OnAttached and OnDetaching methods Call the InvokeActions method in response to stimulus Implementing actions: Derive from the TriggerAction class Override the Invoke method Implementing targeted actions: Derive from the TargetedTriggerAction class Override the Invoke method and use the Target property

Lesson 3: Implementing Drag-and-Drop User Interfaces Implementing Drag-and-Drop Behavior in a WPF Application Implementing Drag-and-Drop Behavior Between Application Instances

Implementing Drag-and-Drop Behavior in a WPF Application Respond to user input: Handle mouse events or use the Thumb control Initiate drag operation: Create DataObject for drag data Call DragDrop.DoDragDrop method DataObject data = new DataObject( "Custom", this.dragData); DragDrop.DoDragDrop( this.dragSource, data, DragDropEffects.Copy); DataObject data = new DataObject( "Custom", this.dragData); DragDrop.DoDragDrop( this.dragSource, data, DragDropEffects.Copy); Complete the drag-and-drop operation: Set the AllowDrop property on the drop target Handle the Drop event

Implementing Drag-and-Drop Behavior Between Application Instances Providing visual feedback during a drag-and-drop operation Use default cursors Define custom cursors Provide custom visual feedback: Use default cursors Define custom cursors Provide custom visual feedback: Adorner-based Window-based Adorner-based Window-based a a b b

Lab: Implementing Drag-and-Drop Operations Exercise 1: Implementing Drag-and-Drop Operations Exercise 2: Implementing Expression Blend Behaviors Logon information Estimated time: 75 minutes

Lab Scenario You have been asked to update the master view for all work orders in the system to provide drag-and-drop support so that the user can drag individual work orders from the Statistics panel onto a scratch pad. You have also been asked to implement a drag-and-drop behavior for Expression Blend to enable other application developers to reuse the drag-and-drop functionality.

Lab Review Review Questions How do you add behavior to the attached element in an attached behavior? Which event do you handle to provide custom visual feedback during a drag-and-drop operation? How do you constrain the type of elements to which you can apply an Expression Blend behavior?

Module Review and Takeaways Review Questions Common Issues and Troubleshooting Tips