Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the window from a page

Tags:

c#

.net

wpf

How to get a window from a page , so I've got a page frame in my window :

<Frame NavigationUIVisibility="Hidden" Name="frmContent" Source="Page/Page1.xaml" OverridesDefaultStyle="False" Margin="0,0,0,0"  />

And trying to access my window from this page this way :

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    if ((Window1)this.Parent == null)
        System.Windows.Forms.MessageBox.Show("111");
    else
    wb1.ObjectForScripting = new MyScriptObject((Window1)this.Parent);

But the Parent returns null , so I see "111" message,

Where is my mistake and how to get window object correct ?

like image 231
cnd Avatar asked Jun 21 '10 04:06

cnd


1 Answers

The parent of the page will be the Frame, not the Window.

The easiest way is to use the Window.GetWindow static method:

var wnd = Window.GetWindow(this);
like image 64
Matt Hamilton Avatar answered Oct 01 '22 21:10

Matt Hamilton