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

7

本文介绍了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:如何设置用户控件显示的对话框的所有者窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14

具有未知类型的 CreateDelegate
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)...
2023-11-11 C#/.NET开发问题
5

Func&lt;T&gt;.BeginInvoke 使用线程池吗?
Does Funclt;Tgt;.BeginInvoke use the ThreadPool?(Funclt;Tgt;.BeginInvoke 使用线程池吗?)...
2023-11-11 C#/.NET开发问题
6

如何为具有空目标的实例方法创建委托?
How to create a delegate to an instance method with a null target?(如何为具有空目标的实例方法创建委托?)...
2023-11-11 C#/.NET开发问题
6