在 WPF 中的 UserControl 内设置窗口所有者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18306043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:27:51  来源:igfitidea点击:

Set Window owner inside of an UserControl in WPF

c#wpfwindowparentowner

提问by FukYouAll

I have an UserControlthat shows a window without an icon in the Taskbar, the problem is when the main window lost the focus and get it again the window without taskbar icon is behind the main window and is difficult show it in front again, this issue is solved using window.Owner = this;when the class is a window, but when is UserControlor other type (e.g. ListBoxItem, Viewbox) I can't do this. I tried to do this control.Owner = (Window)this;but the result throws an exception that a Gridcannot be converted into a Window

我有一个UserControl在任务栏中显示没有图标的窗口,问题是当主窗口失去焦点并再次获得它时,没有任务栏图标的窗口在主窗口后面并且很难再次显示在前面,这个问题是window.Owner = this;当类是窗口时使用解决了,但是当是UserControl或其他类型(例如ListBoxItemViewbox)我不能这样做。我试图这样做,control.Owner = (Window)this;但结果引发了一个Grid无法转换为 a的异常Window

回答by Stephen

use

window.Owner = Window.GetWindow(this);

回答by yo chauhan

 for (int  index=0;index< App.Current.Windows.Count;index++ )
        {
            if (App.Current.Windows[index].Title == "MyWindow")
                control.Owner = App.Current.Windows[index];
        } 

here Title is the Title of Window which you want to set as owner.

这里的标题是您要设置为所有者的窗口标题。