WPF EventRouting to Children
- by Shaun Bowe
Is there any way to broadcast a RoutedEvent to all of my children in WPF?
For example lets say I have a Window that has 4 children. 2 of them know about the RoutedEvent 'DisplayYourself' and are listening for it. How can I raise this event from the window and have it sent to all children?
I looked at RoutingStrategy and Bubble is the wrong direction, Tunnel and Direct don't work because I don't know which children I want to send this to. I just want to broadcast this message and have whoever cares about it handle it.
update:
I declared the events in a static class.
public static class StaticEventClass
{
public static readonly RoutedEvent ClickEvent =
EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(StaticEventClass));
public static readonly RoutedEvent DrawEvent =
EventManager.RegisterRoutedEvent("Draw", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(StaticEventClass));
}
The problem is when I raise the event from my window, the children never see it.
RoutedEventArgs args = new RoutedEventArgs(StaticEventClass.DrawEvent, this);
this.RaiseEvent(args);
update again.. Here is the handler in the child.
public ChildClass()
{
this.AddHandler(StaticEventClass.DrawEvent, new RoutedEventHandler(ChildClass_Draw));
}