actions

Actions
B ro wn Ba g Se m i n a r
S u m m e r 2007
Actions
Often we want an application to respond in some way
when a user moves the mouse, clicks a button, presses a
key, or draws with a pen.
Windows Presentation Foundation has three common
ways of dealing with actions:





Events
Commands
Triggers
We will look at the principles that apply to all three of
these mechanisms, and then dig into the details about
each one.

2
Actions – Brown Bag Seminar
Action Principles
element composition
 loose coupling
 declarative actions

3
Actions – Brown Bag Seminar
Element Composition
Remember the three principles of the control model




Element composition
rich content everywhere
simple programming model
Example of simple programming model

Button b = new Button();
b.Content = "Click Me";
b.Click += delegate { MessageBox.Show("You clicked me!"); };
4
Actions – Brown Bag Seminar
Element Composition

Its not the button itself that is being clicked, but rather the
elements that make up the display of the button

Modified version of previous code snippet
Button b = new Button();
b.Content = new Button();
b.Click += delegate { MessageBox.Show("You clicked me!"); };
Concept of routed events

5
Actions – Brown Bag Seminar
Element Composition
6
Actions – Brown Bag Seminar
Loose Coupling
Semantic events (click) vs. physical events (MouseUp,
MouseDown)
Writing code against the Click event has two advantages




We don’t tie ourselves to a specific input gesture (mouse versus
keyboard)
We don’t tie ourselves to a button.
CheckBox, RadioButton, Button, and Hyperlink all support
clicking. Code written against the Click event depends only on
a component that can be clicked. This decoupling of code to
the action produced allows for more flexibility in the
implementation of handlers

7
Actions – Brown Bag Seminar
Loose Coupling

Events themselves, though, suffer from a form of coupling that
requires the method implementation to be of a specific
signature.

For example, the delegate for Button.Click is defined as
follows:
public
delegate
void
RoutedEventArgs e);
8
RoutedEventHandler(object
Actions – Brown Bag Seminar
sender,
Loose Coupling
One of the goals in WPF is to allow a spectrum of actions,
ranging from tightly coupled physical events (like MouseUp) all
the way to completely semantic notifications (like the
ApplicationCommands.Close command that signals that a
window should be closed).

9
Actions – Brown Bag Seminar
Loose Coupling
template for a window that adds chrome for closing the window
10
Actions – Brown Bag Seminar
Loose Coupling
Loose coupling provides a complete abstraction from the action source (in
this case, the button) and the action handler (in this case, the window).
Leveraging this loose coupling, we could change the window style to use a
totally different control and not break anything.
11
Actions – Brown Bag Seminar
Declarative Actions

WPF is moving toward a model in which software declares its intent
e.g., I want the window to close when you issue this command

instead of its implementation
e.g., Call Window.Close() when you click this button



Events allow us to declare the target function in markup, but the
handler must be implemented in code.
Commands are specifically designed for declarative use, providing the
best abstraction between the action source and consumer.
Triggers provide probably the richest declarative support, but their lack
of extensibility makes them difficult to use for complex tasks.
12
Actions – Brown Bag Seminar
Events



In WPF, events behave exactly as they do in any other .NET
class library.
Each object exposes a set of events to which we can attach a
listener using a delegate.
There are three types of routed events:



13
Direct
Bubbling
tunneling events
Actions – Brown Bag Seminar
Direct Events



Direct events are simple events that fire on a single source; these are
nearly identical to standard .NET events, with the exception that they
are registered with the WPF routed-event system.
The routed-event system is the part of WPF that is responsible for
routing events through the element tree. This system is mostly hidden,
with only small parts of it, like EventManager, RegisterRoutedEvent
visible.
Certain features in the platform (triggers, for example) require an event
to be registered to be used.
14
Actions – Brown Bag Seminar
Bubbling and Tunneling Events
15
Actions – Brown Bag Seminar
Bubbling and Tunneling Events



Tunneling events travel from the root of the element tree to a target
element, and bubbling events do the opposite.
Typically, these two types of events are paired, and the tunneling version is
prefixed with Preview.
Most input events (keyboard, mouse, and pen) have bubbling and
tunneling versions of each event for example, MouseRightButtonDown
and PreviewMouseRightButtonDown, respectively.
16
Actions – Brown Bag Seminar
Commands


Used to deal with things more abstractly.
Suppose we want to add the ability to quit
We will first define the menu in the markup file:
In the code behind, we can implement the event handler:
17
Actions – Brown Bag Seminar
Commands

This works fine, but now lets also add a text section that includes a
hyperlink to exit the application:

We’re making a lot of assumptions about the implementation of
ExitClicked.


18
the signature is compatible with the Hyperlink.Click
We now have arbitrary methods in the code behind baked into the markup file,
so a graphic designer trying to build the UI for this application would have no
idea what event handlers to bind to.
Actions – Brown Bag Seminar
Commands



Commands exist to help solve this problem.
They allow us to provide a single name to signify the action that we
want.
To start using commands, must do three things:



19
define a command
define the implementan of that command
create a trigger for the command
Actions – Brown Bag Seminar
Commands

Defining a new command:
20
Actions – Brown Bag Seminar
Commands

To bind a menu item or hyperlink to Exit command:
21
Actions – Brown Bag Seminar
Commands
22
Actions – Brown Bag Seminar
Trigger

Triggers are signaled by three things:
The state of a display property (Trigger)
 The state of a data property (DataTrigger)
 An event (EventTrigger)
Allthree trigger types cause a set of actions when they are signaled

23
Actions – Brown Bag Seminar
Adding Triggers to Data
24
Actions – Brown Bag Seminar
Adding Triggers to Data
25
Actions – Brown Bag Seminar
Adding Triggers to Data
26
Actions – Brown Bag Seminar
Adding Triggers to Data
27
Actions – Brown Bag Seminar