duminică, 31 octombrie 2010

[1][1] Routed Events

[1][1] Routed Events

  • Events in WPF are called routed because there is a brand new process to handle these events.
  • In WPF, the parent element can provide information to child elements, providing usage, customization, and visibility to the potentially smaller, nested objects.
  • Instead of just calling handlers attached to the element that raised the event, WPF walks the tree of user interface elements, calling all handlers for the routed event attached to any node from the originating element right up to the root of the user interface tree.
  • The C# compiler would generate a hidden private field to hold the event handler, meaning that you pay a per-object overhead for each event whether or not any handlers are attached.
  • There are three types of routed events:
    • Tunneling
      • It fires in the opposite direction from the top of the visual tree down.
      • Tunneling events can be easily identified because, by convention, they are named with a Preview prefix
    • Bubbling
      • It provides event handling from the originating element of a visual tree to the root of the tree.
    • Direct
      • Direct events represent the typical approach to which .NET programmers
  • To suppress the handling of an event, you need to set that Handled property of the RoutedEventArgs parameter

private void PreviewMouseDownButton(object sender, RoutedEventArgs e)
    e.Handled = true;

    • Although setting the RoutedEventArgs parameter’s Handled property to true in a routed event handler appears to stop the tunneling or bubbling, individual handlers further up or down the tree can opt to receive the events anyway
    • This can only be done from procedural code, using an overload of AddHandler that adds a Boolean handledEventsToo parameter
    • If you set the Handled flag in a Preview handler, not only will the tunneling of the Preview event stop, but also the corresponding bubbling event that would normally follow will not be raised at all.
  • Find the Source Element in an Event Handler: you need to access the Source property of the RoutedEventArgs parameter
  • These are the properties of RoutedEventArgs that are most useful:
    • Source—The element in the logical tree that originally raised the event.
    • OriginalSource—The element in the visual tree that originally raised the event
    • Handled
    • RoutedEvent—The actual routed event object (such as Button.ClickEvent), which can be helpful for identifying the raised event when the same handler is used for multiple routed events.
  • Routed events are represented as public static RoutedEvent fields with a conventional Event suffix
  • The routed event is registered much like a dependency property in the static constructor and a normal .NET event—or event wrapper—is defined to enable more familiar use from procedural code and adding a handler in XAML with event attribute syntax
    • There are used AddHandler and RemoveHandler.
      • These methods are inherited from System.Windows.UIElement
    • static Button()
      // Register the event
      Button.ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Button));
      // A .NET event wrapper (optional)
      public event RoutedEventHandler Click
      add { AddHandler(Button.ClickEvent, value); }
      remove { RemoveHandler(Button.ClickEvent, value); }
      protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)

      // Raise the event
      RaiseEvent(new RoutedEventArgs(Button.ClickEvent, this));

  • Most of the bubbling events are used in pair with tunneling event
    • The idea behind having a pair of events for various activities is to give elements a chance to effectively cancel or otherwise modify an event that’s about to  occur.

Attached Events

  • This is the routed-event equivalent of an attached property: an event defined by a different class than the one from which the event will be raised.
  • It keeps system open for extension

  • <Grid Mouse.PreviewMouseDown="PreviewMouseDownGrid" Mouse.MouseDown="MouseDownGrid">
  • The code behind it is :
    _grid.AddHandler(Mouse.PreviewMouseDownEvent, new MouseButtonEventHandler(PreviewMouseDownGrid));
    _grid.AddHandler(Mouse.MouseDownEvent, new MouseButtonEventHandler(MouseDownGrid));


    Mouse.AddPreviewMouseDownHandler(_grid, PreviewMouseDownGrid);
    Mouse.AddMouseDownHandler(_grid, MouseDownGrid);

Mouse Inputs

Element captured the mouse.
Element lost mouse capture.
Mouse pointer moved into element.
Mouse pointer moved out of element.
PreviewMouseLeftButtonDown, MouseLeftButtonDown
Tunnel, Bubble
Left mouse button pressed while pointer inside element.
PreviewMouseLeftButtonUp, MouseLeftButtonUp
Tunnel, Bubble
Left mouse button released while pointer inside element.
PreviewMouseRightButtonDown, MouseRightButtonDown
Tunnel, Bubble
Right mouse button pressed while pointer inside element.
PreviewMouseRightButtonUp, MouseRightButtonUp
Tunnel, Bubble
Right mouse button released while pointer inside element.
Tunnel, Bubble
Mouse button pressed while pointer inside element (raised for any mouse button).
Tunnel, Bubble
Mouse button released while pointer inside element (raised for any mouse button).
PreviewMouseMove, MouseMove
Tunnel, Bubble
Mouse pointer moved while pointer inside element.
PreviewMouseWheel, MouseWheel
Tunnel, Bubble
Mouse wheel moved while pointer inside element.
Mouse cursor shape to be determined while pointer inside ele


Niciun comentariu:

Trimiteți un comentariu