.net – WPF: How to set the Owner Window of a Dialog shown by a UserControl


I've got a WPF application with these three types of things…

  • WindowMain
  • UserControlZack
  • WindowModal

UserControlZack1 sits on my WindowMain…

<Window x:Class="WindowMain"
        <local:UserControlZack x:Name="UserControlZack1" ... />

UserControlZack1 displays a WindowModal dailog box…

Partial Public Class UserControlZack


    Private Sub SomeButton_Click(...)
        'instantiate the dialog box and open modally...
        Dim box As WindowModal = New WindowModal()
        box.Owner = ?????
        'process data entered by user if dialog box is accepted...
        If (box.DialogResult.GetValueOrDefault = True) Then
            _SomeVar = box.SomeVar
        End If
    End Sub

End Class

How do I set box.Owner to the correct Window, my running instance of WindowMain?

I cannot use box.Owner = Me.Owner, because "'Owner' is not a member of 'ProjectName.UserControlZack'."

I cannot use box.Owner = Me.Parent, because that returns a Grid, not the Window.

I cannot use box.Owner = WindowMain, because "'WindowMain' is a type and cannot be used as an expression."

Best Solution

Try to use

.Owner = Window.GetWindow(this)