I donโt know what exactly you want to know. Quite simple: you can register a handler at the instance (object) level or at the class level.
The difference is that when you register an event at the class level, it will be called first before all instance-level handlers (of course, tunneling or bubbling still occurs if the processing class is lower / higher in the logical tree). Thus, you can handle this event at the class level and filter whether this event should be handled by the instance or not (by setting e.Handled = true you will stop the event to pass through other handlers). This may be useful in some cases, but at the moment I have no example to share.
In this example, an event handler will be registered, which will be called only when the event was created for a specific instance of the element:
DockPanel panel = new DockPanel(); panel.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click));
And this will create an event handler that will be called every time any DockPanel receives a Button.Click event before the instance handler of this DockPanel is called:
EventManager.RegisterClassHandler(typeof(DockPanel), Button.ClickEvent, new RoutedEventHandler(ButtonClass_Click));
If there were the following methods:
private void ButtonClass_Click(object sender, RoutedEventArgs e) { Debug.Write("Class level handler"); } private void Button_Click(object sender, RoutedEventArgs e) { Debug.Write("Instance level handler"); }
This will produce the output:
Class level handler
Instance level handler
But if you specify args handeled arguments ( e.Handled = true; ) in the level handler, it will filter this event for the level handler (and bubble in the logical tree).
Pako
source share