Chapter 7 Actions
Up to now we have focused on the output side of the platform, looking at how an application is built up from controls that use visuals to display and layout to position everything.
Now we can start to look at the input side of the platform.
WPF Actions: events, commands, and triggers.
Action Principles: element composition, loose coupling, and declarative actions.
7.1 Action Principles
To allow actions to work with the way that WPF builds the display tree, with many elements that may be further composed of other elements, requires the use of element composition.
routed event 原来是来自于 element composition 引入的 event 处理难题
"Routed events are events that are designed to work well with a tree of elements ."
- "Important New Concepts in WPF "
7.2 Events
There are three types of routed events: direct, bubbling, and tunneling events.
Tunneling and bubbling work great for events that are built into every control, like mouse events; however, any event can tunnel or bubble.
To allow for both tunneling and bubbling, WPF supports attached events.
对 routing event 支持到了极致
Every event that uses the event system in WPF has a RoutedEvent field on the defining object, just like the Dependency-Property field that is used for the property system.
疑问, 这个方法好么?
7.3 Commands
简单点说, 就是一个通用的 Event Handler
Commands allow for a loose coupling between the UI and the behavior.
7.4 Triggers
Triggers are signaled by three things: (1) the state of a display property (Trigger), (2) the state of a data property (DataTrigger), or (3) an event (EventTrigger).
7.4.1 Adding Triggers to Data
Triggers can be used only inside of a template or style.
通过 Trigger 可以在绑定时做一些简单的检查, 设置属性工作, 从而赋予 XAML 一定的编程能力.
既然这样, 何不直接像 HTML, 引入 javascript 之类作为界面语言?
By using DataTrigger, we could move all the UI-dependent logic into the markup and leave the minimal amount of code in the code.
7.4.2 Adding Triggers to Controls
the design for Trigger and EventTrigger was inspired by the desire to have all view-dependent logic of a control declared in the markup.
7.4.3 Triggers as the New "if"
Triggers are a simple form of rules engine.