WPF EventRouting to Children

Posted by Shaun Bowe on Stack Overflow See other posts from Stack Overflow or by Shaun Bowe
Published on 2009-01-14T21:30:04Z Indexed on 2010/05/06 21:08 UTC
Read the original article Hit count: 312

Filed under:
|
|
|
|

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));
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about events