WPF:如何设置UserControl显示的对话框的所有者窗口? [英] WPF: How do I set the Owner Window of a Dialog shown by a UserControl?
问题描述
我有一个这些三种类型的WPF应用程序...
I've got a WPF application with these three types of things...
- WindowMain
- UserControlZack
- WindowModal
UserControlZack1位于我的WindowMain ...
UserControlZack1 sits on my WindowMain...
<Window x:Class="WindowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ProjectName"
...
Name="WindowMain">
<Grid>
...
<local:UserControlZack x:Name="UserControlZack1" ... />
...
</Grid>
</Window>
UserControlZack1显示一个WindowModal dailog框...
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 = ?????
box.ShowDialog()
'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
如何将box.Owner设置为正确的Window,WindowMain的运行实例?
How do I set box.Owner to the correct Window, my running instance of WindowMain?
我不能使用 box.Owner = Me。所有者
,因为所有者不是ProjectName.UserControlZack的成员。
I cannot use box.Owner = Me.Owner
, because "'Owner' is not a member of 'ProjectName.UserControlZack'."
我不能使用 box.Owner = Me.Parent
,因为它返回一个网格而不是窗口。
I cannot use box.Owner = Me.Parent
, because that returns a Grid, not the Window.
我不能使用框.Owner = WindowMain
,因为WindowMain是一个类型,不能用作表达式。
I cannot use box.Owner = WindowMain
, because "'WindowMain' is a type and cannot be used as an expression."
推荐答案
尝试使用
.Owner = Window.GetWindow(this)
这篇关于WPF:如何设置UserControl显示的对话框的所有者窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!