toni
toni

Reputation: 31

WPF How to get window reference that is loaded from a page that is into it?

I have a page that is into a window similiar to MDI. I want to get the reference of the window in what the page is placed into. The window is loaded.

Thanks.

Upvotes: 3

Views: 2205

Answers (2)

Jeff
Jeff

Reputation: 8148

I ran into a similar problem and I started with Richard's solution (+1 vote from me!), but I found I had to cast the type to my specific window type to do anything useful.

MainWindow w = (MainWindow)Window.GetWindow(this);
w.method_to_run();

Upvotes: 1

Richard
Richard

Reputation: 3367

I'm not 100% sure if I am understanding exactly what you mean.... if you're talking about a Windows Application that where a User Control is situated in a Window, and you want to do something to the Window from the User Control, you can simply do this (in this example it just closes the Window):

Window window = Window.GetWindow(this);
if (window != null) 
    window.Close();

If this isn't what you meant, could you please post a bit more detail.

Upvotes: 7

Related Questions