RoutedEventArgs vs EventArgs

前端 未结 3 1835
说谎
说谎 2020-12-30 02:15

I am learning WPF / Silverlight and saw in an MS vidcast that it is now recommended to use RoutedEventArgs over EventArgs; although it didn\'t say

相关标签:
3条回答
  • 2020-12-30 02:47

    Well, basically a RoutedEvent travels through the Logical tree, either from the source element to root element (Bubble event route) or, less frequently from root element to sub levels elements (Tunnel event route). What this means is that if you have a Button inside of a StackPanel, that itself is inside of a Grid; if you define a Click event in the controls they will all trigger it unless one of them handles it.

    If the event route is Bubble (named as a regular event Click), it will go:

    Button -> StackPanel -> Grid

    If the event route is Tunnel (named PreviewClick), it will go the other way around:

    Grid -> StackPanel -> Button

    So now with the handling, it's pretty simple. If it's a Bubble route and the Button sets the RoutedEventArgs.Handled to true, that StackPanel and Grid will not trigger it. Same with the RoutedEvent, if the Grid handles it, the StackPanel and Button will not trigger it.

    This is my understanding in a nutshell, I avoided some stuff for simplicity.

    I recommend this chapter for better understanding of this WPF feature.

    0 讨论(0)
  • 2020-12-30 02:49

    Say we have a Button element containing other elements, a StackPanel, itself containing a TextBox and an Image element.

    The Button element should be able to handle a click event no matter if the Image that got clicked or if the TextBox was.

    Hence WPF provides a way to :

    • Propagate an event through element tree most of the time bubbling from the source element (here say the Image), to a higher level toward root element (for instance the button here).
    • Handle such a propagated event.
    0 讨论(0)
  • 2020-12-30 03:02

    RoutedEventArgs is a new type of event argument that exists to support the WPF model of eventing: Routed Events. It's hard to explain in a short entry why exactly WPF chose this model or what the point is so I'll start by pointing you to a good article on the subject.

    • http://msdn.microsoft.com/en-us/library/ms742806(VS.85).aspx
    0 讨论(0)
提交回复
热议问题