cnd
cnd

Reputation: 33754

Get the window from a page

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 ?

Upvotes: 20

Views: 15522

Answers (1)

Matt Hamilton
Matt Hamilton

Reputation: 204139

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

Upvotes: 49

Related Questions