KEMBAR78
Lesson 07 Actions and Commands in WPF | PPTX
 User actions
 moves the mouse, clicks a button, presses a key,
or draws with a pen,…
 WPF provides
 events, commands, and triggers
 Functional definition: A routed event is a type
of event that can invoke handlers on multiple
listeners in an element tree, rather than just
on the object that raised the event.
 Implementation definition: A routed event is a
CLR event that is backed by an instance of
the RoutedEvent class and is processed by
theWPF event system.
 The event route for this Click event is:
Button-->StackPanel-->Border-->...
 Control composition and encapsulation
 Singular handler attachment points
 Attach handler one time for multiple elements
 Class handling
 Permit a static handler that is defined by the class
 Referencing an event without reflection
 Event identification that does not require static or
run-time reflection
 Bubbling: routes from element to root
 Direct: only source element invoke handlers
 Tunneling: routes from root to element
 Attached event allows you to attach an event
handler to some child element rather than to
the parent that actually defines the event
 Attached event enable elements to handle
events that are declared in a different
element
 Attached events are routed events that
support a hookup in XAML on elements other
than the type on which the event is declared
 Event Button.Click is attached for Grid element
Routed Events
 EventSetters
 Represented by the <EventSetter> tag that pre-
declared in Styles
 Sharing handler for multiple elements by Styles
 EventTriggers
 Represented by the <EventTrigger> tag that pre-
declared in Styles
 Specify an event and activate when that event is
raised
 Have an Actions collection
 Commands specify what can application do?
 Command is an action the application
performs at the user’s request
 Single handler for a command can enables for
multiple elements
 Command allows multiple and disparate
sources to invoke the same command logic
 Command example:
 Copy: Ctrl+C, EditCopy, Copy Button,...
 The command is the action to be executed.
 The command source is the object which
invokes the command.
 The command target is the object that the
command is being executed on.
 The command binding is the object which
maps the command logic to the command.
 A command object identifies a particular
command (such as copy or paste)
 Commands are created by implementing the
ICommand interface
Class Command types
ApplicationCommands Commands common to almost all applications. Includes
clipboard commands, undo and
redo, and document-level operations (open, close, print,
etc.).
ComponentCommands Operations for moving through information, such as scroll
up and down, move to end, and
text selection.
EditingCommands Text editing commands such as bold, italic, center, and
justify.
MediaCommands Media-playing operations such as transport (play, pause,
etc.), volume control, and track
selection.
NavigationCommands Browser-like navigation commands such as Back, Forward,
and Refresh.
 A command source is the object which
invokes the command (such as MenuItem,
Button,…)
 Command sources generally implement the
ICommandSource interface
 A CommandBinding associates a command
with the event handlers that implement the
command
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Open"
Executed="OpenCmdExecuted"
CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
 The command target is the element on which
the command is executed
 The command source can explicitly set the
command target
 If the command target is not defined, the
element with keyboard focus will be used as
the command target
 invoke a command on multiple targets without
having to keep track of the command target
 Implement the ICommand interface
 Create a RoutedCommand
 Create a RoutedUICommand.
Custom RoutedCommand
 Style, ControlTemplate, and DataTemplate
all have a Triggers property that can contain a
set of triggers.
 A trigger sets properties or starts actions
such as animation when a property value
changes or when an event is raised
 Used to apply setter elements to control by
conditions of dependency properties
<StyleTargetType="{x:Type Button}">
<Setter Property="Button.Background"Value="AliceBlue" />
<Style.Triggers>
<Trigger Property="IsMouseOver"Value="True">
<Setter Property="Button.Opacity"Value="1"></Setter>
<Setter Property="Button.Background"Value="Green"></Setter>
</Trigger>
</Style.Triggers>
</Style>
MultipleTriggers
Multiconditions
 Used to apply setter elements to control by
conditions of CLR properties (data binding)
 Also have multiple and multicondition
<StyleTargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Role}"Value="Admin">
<Setter Property="Foreground"Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
 Used to raising an animation to control by
watch for events
<EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="MyText">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="Popup"
Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="00:00:00"Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
Triggers

Lesson 07 Actions and Commands in WPF

  • 2.
     User actions moves the mouse, clicks a button, presses a key, or draws with a pen,…  WPF provides  events, commands, and triggers
  • 4.
     Functional definition:A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.  Implementation definition: A routed event is a CLR event that is backed by an instance of the RoutedEvent class and is processed by theWPF event system.
  • 5.
     The eventroute for this Click event is: Button-->StackPanel-->Border-->...
  • 6.
     Control compositionand encapsulation  Singular handler attachment points  Attach handler one time for multiple elements  Class handling  Permit a static handler that is defined by the class  Referencing an event without reflection  Event identification that does not require static or run-time reflection
  • 7.
     Bubbling: routesfrom element to root  Direct: only source element invoke handlers  Tunneling: routes from root to element
  • 8.
     Attached eventallows you to attach an event handler to some child element rather than to the parent that actually defines the event  Attached event enable elements to handle events that are declared in a different element  Attached events are routed events that support a hookup in XAML on elements other than the type on which the event is declared
  • 9.
     Event Button.Clickis attached for Grid element
  • 10.
  • 11.
     EventSetters  Representedby the <EventSetter> tag that pre- declared in Styles  Sharing handler for multiple elements by Styles  EventTriggers  Represented by the <EventTrigger> tag that pre- declared in Styles  Specify an event and activate when that event is raised  Have an Actions collection
  • 15.
     Commands specifywhat can application do?  Command is an action the application performs at the user’s request  Single handler for a command can enables for multiple elements  Command allows multiple and disparate sources to invoke the same command logic  Command example:  Copy: Ctrl+C, EditCopy, Copy Button,...
  • 16.
     The commandis the action to be executed.  The command source is the object which invokes the command.  The command target is the object that the command is being executed on.  The command binding is the object which maps the command logic to the command.
  • 17.
     A commandobject identifies a particular command (such as copy or paste)  Commands are created by implementing the ICommand interface
  • 18.
    Class Command types ApplicationCommandsCommands common to almost all applications. Includes clipboard commands, undo and redo, and document-level operations (open, close, print, etc.). ComponentCommands Operations for moving through information, such as scroll up and down, move to end, and text selection. EditingCommands Text editing commands such as bold, italic, center, and justify. MediaCommands Media-playing operations such as transport (play, pause, etc.), volume control, and track selection. NavigationCommands Browser-like navigation commands such as Back, Forward, and Refresh.
  • 19.
     A commandsource is the object which invokes the command (such as MenuItem, Button,…)  Command sources generally implement the ICommandSource interface
  • 20.
     A CommandBindingassociates a command with the event handlers that implement the command <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Open" Executed="OpenCmdExecuted" CanExecute="OpenCmdCanExecute"/> </Window.CommandBindings>
  • 21.
     The commandtarget is the element on which the command is executed  The command source can explicitly set the command target  If the command target is not defined, the element with keyboard focus will be used as the command target  invoke a command on multiple targets without having to keep track of the command target
  • 23.
     Implement theICommand interface  Create a RoutedCommand  Create a RoutedUICommand.
  • 24.
  • 26.
     Style, ControlTemplate,and DataTemplate all have a Triggers property that can contain a set of triggers.  A trigger sets properties or starts actions such as animation when a property value changes or when an event is raised
  • 27.
     Used toapply setter elements to control by conditions of dependency properties <StyleTargetType="{x:Type Button}"> <Setter Property="Button.Background"Value="AliceBlue" /> <Style.Triggers> <Trigger Property="IsMouseOver"Value="True"> <Setter Property="Button.Opacity"Value="1"></Setter> <Setter Property="Button.Background"Value="Green"></Setter> </Trigger> </Style.Triggers> </Style>
  • 28.
  • 29.
     Used toapply setter elements to control by conditions of CLR properties (data binding)  Also have multiple and multicondition <StyleTargetType="{x:Type ListBoxItem}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=Role}"Value="Admin"> <Setter Property="Foreground"Value="Red" /> </DataTrigger> </Style.Triggers> </Style>
  • 30.
     Used toraising an animation to control by watch for events <EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="MyText"> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup" Storyboard.TargetProperty="(Popup.IsOpen)"> <DiscreteBooleanKeyFrame KeyTime="00:00:00"Value="True"/> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger>
  • 31.

Editor's Notes

  • #7 Top-level Scenarios for Routed Events The following is a brief summary of the scenarios that motivated the routed event concept, and why a typical CLR event was not adequate for these scenarios: Control composition and encapsulation: Various controls in WPF have a rich content model. For example, you can place an image inside of a Button, which effectively extends the visual tree of the button. However, the added image must not break the hit-testing behavior that causes a button to respond to a Click of its content, even if the user clicks on pixels that are technically part of the image. Singular handler attachment points: In Windows Forms, you would have to attach the same handler multiple times to process events that could be raised from multiple elements. Routed events enable you to attach that handler only once, as was shown in the previous example, and use handler logic to determine where the event came from if necessary. For instance, this might be the handler for the previously shown XAML: Class handling: Routed events permit a static handler that is defined by the class. This class handler has the opportunity to handle an event before any attached instance handlers can. Referencing an event without reflection: Certain code and markup techniques require a way to identify a specific event. A routed event creates a RoutedEvent field as an identifier, which provides a robust event identification technique that does not require static or run-time reflection.
  • #8 Bubbling: Event handlers on the event source are invoked. The routed event then routes to successive parent elements until reaching the element tree root. Most routed events use the bubbling routing strategy. Bubbling routed events are generally used to report input or state changes from distinct controls or other UI elements. Direct: Only the source element itself is given the opportunity to invoke handlers in response. This is analogous to the "routing" that Windows Forms uses for events. However, unlike a standard CLR event, direct routed events support class handling (class handling is explained in an upcoming section) and can be used by EventSetter and EventTrigger. Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event). Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control. Input events provided in WPF often come implemented as a tunneling/bubbling pair. Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.
  • #17 The routed command model in WPF can be broken up into four main concepts: the command, the command source, the command target, and the command binding: The command is the action to be executed. The command source is the object which invokes the command. The command target is the object that the command is being executed on. The command binding is the object which maps the command logic to the command.