WPF:如何设置用户控件显示的对话框的所有者窗口?

WPF: How do I set the Owner Window of a Dialog shown by a UserControl?(WPF:如何设置用户控件显示的对话框的所有者窗口?)
本文介绍了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:如何设置用户控件显示的对话框的所有者窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)