views:

459

answers:

2

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

If you create a routed event like "MyWindow.MyRoutedEvent" you just created an event handler on your children that listen for the event. Since the RoutedEvent is declared as static on your window you have access to it from other windows.

Micah
Thanks for the answer. I did declare the event as static. I just can't get the children to hear the event? It never gets sent to them.
Shaun Bowe
can you show me the eventhandler code in the children?
Micah
I updated the code in the question.. Thanks again
Shaun Bowe
children never listen!!!!
Janie
A: 

Shaun:

I think the problem is that you don't want a routed event. Just put a regular old public clr event on the static class and attach anything you want to it.

David

David