Saturday, January 09, 2010

01/08

 

 

 

WPF DP:

 

In a nutshell, dependency properties provide the following benefits above and beyond the
simple data encapsulation found with a CLR property:
• Dependency properties can inherit their values from a parent element’s XAML definition.
• Dependency properties support the ability to have values set by external types (recall from Chapter 28 that attached properties do this very thing, as attached properties are based on dependency properties).
• Dependency properties allow WPF to compute a value based on multiple external values.
• Dependency properties provide the infrastructure for callback notifications and triggers (used quite often when building animations, styles, and themes).
• Dependency properties allow for static storage of their data (which helps conserve memory consumption).

 

So

 

  • DP can allows WPF to compute a value based on
    values from multiple property inputs
  • DP can be configured to monitor changes
    of the property value to force external actions to occur(Trigger event)

When to create a customized DP:

 

if you are building a custom WPF control, where you have subclassed an existing control to modify its behaviors. In this case, if you are creating a property that needs to work with the WPF data-binding
engine, theme engine, or animation engine, or if the property must broadcast when it has changed, a dependency property is the correct course of action

 

 

Routed Events

注册路由事件时,我们可以选择不同的路由策略。

  • 管道传递(Tunneling): 事件首先在根元素上触发,然后向下层级传递,直到那个最初触发事件的子元素。 In WPF, tunneling events are prefixed by the word Preview by convention.
  • 冒泡(Bubbling): 事件从最初触发事件的子元素向根元素层级往上传递。
  • 直接(Direct): 事件仅在最初触发事件的子元素上触发。
  • Bubbling (Up)
  • Tunneling(down) --- Preview events :
    • have the power to perform any special logic (data validation, disable bubbling action, etc.) before the bubbling counterpart
      fires

Example :

public void routedeventhandler(object sender,routedevent e)

{

//keep  bubbling

e.hanled=false

e.handled=tru

}

 

Routed EVents Order

 

The order of event processing is as follows:

  1. PreviewMouseDown (tunnel) on root element.

  2. PreviewMouseDown (tunnel) on intermediate element #1.

  3. PreviewMouseDown (tunnel) on source element #2.

  4. MouseDown (bubble) on source element #2.

  5. MouseDown (bubble) on intermediate element #1.

  6. MouseDown (bubble) on root element.

 

 

image

0 Comments:

Post a Comment

<< Home