I am entirely new to WPF . Though I am going through the tutorials and articles from net but I am having doubts . Some of the questions which have striked out so far are
What is routed events and what extra thing it does server?
What are dependency properties and it's benefits.
How dependency properties differ from attached properties.
Why does first bubbling happens followed by tunneling? And what is the purpose of this?
e.g.
<canvas>
<button canvas.left=10/>
</canvas>
Is it a dependency property or 开发者_JAVA技巧attached property?
Thanks
- Routed Event is event that design for tree of elements, when routed event raise it can travel up (bubbling) and down (tunneling) the element tree. For example if you have button inside grid, and you click button so route can raise up the tree to grid.
- Dependency Property is another type of property that can notify when its value change, also it can inheritance and support multiple provider.
Attached Property is another form of dependency property that can attach to any kind of object. As I have slow,
TextBlock.FontSize
is attached property<Canvas TextBlock.FontSize="10"> <TextBlock Text="Test" /> </Canvas>
Bubbling is for *_Changing event and tunneling is for *_Changed event. So if you need to validate and handle something you must use *_Changing event, if you validate by *_Changed event you may not handle (cancel) it.
- In your example is attached property.
1 (& 4): Routed Events either bubble up or tunnel down the visual tree. And if someone sets "Handled = true" on the event args along the way then it goes no further. The point is, you don't know who in the visual tree will get the click event (e.g., a button might contain a StackPanel with an Image and a TextBlock...when the button is clicked the event could go to any of these), but with the Routed Event mechanism you have all the control you need over who should handle event. The convention is that Tunnel events have names prefixed with Preview (e.g., PreviewMouseDown is the tunneling version of the MouseDown bubbling event).
2: The thing about dependency properties is that they don't actually 'have' a value - the value at any point 'depends' on various other factors (such as: any styles, triggers, default values, etc). When several of these factors exist, there's an order of precedence which determines which value will be used...see here. This makes it easy to alter the display of an element (e.g., when the mouse is over it) and then change it back to whatever it was previously when the mouse is no longer over it, for example. The values for a dependency property can also be inherited from an ancestor - which is really useful for things like DataContext. So you can set the DataContext of an element and all of it's children will have access to it.
3: Attached properties enable a child element to store a value associated with a property on an ancestor. Like in the example you give the 'left' property belongs to the button's parent, the canvas. Attached properties mean that multiple child elements can store different values for the same property on the ancestor.
I hope this helps...keep digging and asking the questions - it's worth getting your head round this stuff!
精彩评论