0

I am trying to understand how RoutedEvents work. Well - I walked through some tutorials and understood why RoutedEvents are useful and how they work. But there is one thing, that I don't get:

Let's say I wrote a class (e.g. "MyClass") , which has a RoutedEvent property, sth. like this:

public class MyClass 
{
public static readonly RoutedEvent myEvent;
...
}

Well - just giving a property is not enough - so I have to register the RoutedEvent with the help of EventManager:

...
myEvent = EventManager.RegisterRoutedEvent("MyEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyClass));
...

Okay - now the WPF event system knows about THIS event. If I do it that way, each class I write will have it's own RoutedEvent. But that makes no sense to me.

What I want, is that other classes listen to the same event - without being a type of MyClass.

For example: I have a stackpanel and within the stackpanel is a button. Clicking the stackpanel will raise the onClick event. Clicking the button will raise the onClick event of the button - and then the onClick event on the stackpanel. But how?

Sorry - it's hard for me to describe the problem - I am just too confused :)

Thx a lot. CodeCannibal

CodeCannibal
  • 324
  • 6
  • 21

1 Answers1

1

What I want, is that other classes listen to the same event - without being a type of MyClass.

You expect the right from this and this is what it delivers. I mean by registering a RoutedEvent you are not strongly binding it to the type; instead you are bridging it using the string "MyEvent" EventManager.RegisterRoutedEvent("MyEvent", ...

RoutedEvent traverse through the logical tree and stops traversing when handled (exceptions are there).

So, StackPanel need not to be derived from MyClass. You just need to register the RoutedEvent at StackPanel by specifying the action/handler. Whenever the RoutedEvent traverse through StackPanel it will call the corresponding action.

For example:

UserControl1.cs

//Routed Event
public static readonly RoutedEvent ThisIsEvent = EventManager.RegisterRoutedEvent("ThisIs", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserControl1));

// .NET wrapper
public event RoutedEventHandler ThisIs
{
    add { AddHandler(ThisIsEvent, value); }
    remove { RemoveHandler(ThisIsEvent, value); }
}

//local handler where RaiseEvent is called
private void button1_Click(object sender, RoutedEventArgs e)
{
    RaiseEvent(new RoutedEventArgs(ThisIsEvent));
}

And below is how you subscribe to that event in you XAML. You can also do this in your code file...

<StackPanel Orientation="Vertical" **local:UserControl1.ThisIs="StackPanel_ThisIs"** >
    <local:UserControl1></local:UserControl1>
</StackPanel>

I hope this clear your doubts.

gaurawerma
  • 1,826
  • 13
  • 16
  • Could you please give me a code example? I still don't get it - in my eyes each class should register a RoutedEvent - but if I want that the event is on the route of another, the name ("MyEvent") must be the same? – CodeCannibal May 21 '12 at 11:57
  • Hey - thanks, that was not what I expected to get, but it helped me understanding my question from the other side :) Well - I think it just needs a little bit trainig - thank you so far :) – CodeCannibal May 22 '12 at 09:27
  • RoutedEvent does not stop traverse the event route (not necessary the logic tree either) after the event is marked as handled. The event still traverse the event route even after marked as handled. However, the event handler will not be invoked unless it is registered specially. – Frank Liu Jan 28 '15 at 22:48