DiamonDogX

I have a window. One of its member variables is also a window. I want the "child" window to be able to raise an event and the parent window receive it. It doesn't seem to work. I'm using the same setup I've used before for raise and handling routed events. I guess it doesn't see it as a UIElement child. Being a member doesn't count as being on the tree Setup is something like this:

Code Snippet

public ParentWindow : Window

{

ChildWindow childWindow;

...

...

this.AddHandler( ChildWindow.AnEvent, new RoutedEventHandler( OnChildWindowEventRaised ) );

...

}

public ChildWindow : Window

{

public static readonly RoutedEvent AnEvent =

EventManager.RegisterRoutedEvent( "AnEventName", RoutingStrategy.Bubble,

typeof( RoutedEventHandler ), typeof( ChildWindow) );

public event RoutedEventHandler AnEventName

{

add { AddHandler( AnEvent , value ); }

remove { RemoveHandler( AnEvent , value ); }

}

...

...

this.RaiseEvent( new RoutedEventArgs( AnEvent ) );

}

How could I accomplish this scenario



Re: Windows Presentation Foundation (WPF) Routed event from one window to another?

Marlon Grech


is there a specific reason why you need a routed event and not a normal event





Re: Windows Presentation Foundation (WPF) Routed event from one window to another?

DiamonDogX

Probably could use a normal event... how would it work in my scenario I just want to know when a particular button was pressed in the child window. I know this is easy and I could just do childWindow.theButton.Click += etc. etc. from the parent window. I was just trying to be elegant about it.




Re: Windows Presentation Foundation (WPF) Routed event from one window to another?

Marlon Grech


create an event in the window

so ...

Code Snippet

class ChildWindow : Window
{
public event EventHandler MyEvent;
}

class ParentWindow : Window
{
private ChildWindow child;

public ParentWindow(ChildWindow child)
{
this.child = child;
child.MyEvent += new EventHandler(test);
}
public void test(object sender, EventArgs e)
{
//do something
}
}




this should do the trick

hope it helps....