WPF:如何设置用户控件显示的对话框的所有者窗口? [英] WPF: How do I set the Owner Window of a Dialog shown by a UserControl?

查看:18
本文介绍了WPF:如何设置用户控件显示的对话框的所有者窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含这三种类型的 WPF 应用程序...

  • 主窗口
  • UserControlZack
  • 窗口模式

UserControlZack1 位于我的 WindowMain...

UserControlZack1 显示一个 WindowModal 对话框...

<上一页>部分公共类 UserControlZack...私有子 SomeButton_Click(...)'实例化对话框并以模态方式打开...暗箱 As WindowModal = New WindowModal()框.所有者 = ?????box.ShowDialog()'如果对话框被接受,则处理用户输入的数据...如果 (box.DialogResult.GetValueOrDefault = True) 那么_SomeVar = box.SomeVar...万一结束子结束类

如何将 box.Owner 设置为正确的 Window,即我正在运行的 WindowMain 实例?

我不能使用 box.Owner = Me.Owner,因为'Owner' 不是 'ProjectName.UserControlZack' 的成员."

我不能使用 box.Owner = Me.Parent,因为它返回的是 Grid,而不是 Window.

我不能使用 box.Owner = WindowMain,因为'WindowMain' 是一种类型,不能用作表达式."

解决方案

尝试使用

.Owner = Window.GetWindow(this)

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

  • WindowMain
  • UserControlZack
  • WindowModal

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

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."

解决方案

Try to use

.Owner = Window.GetWindow(this)

这篇关于WPF:如何设置用户控件显示的对话框的所有者窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆