我最终在这里结合使用了几个答案。首先接受的答案是有用的,但是由于这里的其他人已经指出设置Topmost = true
意味着窗口始终高于任何其他正在运行的应用程序。我的解决办法是这样的:
var myWindow = new MyWindowType();
myWindow.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
我最初使用:
myWindow.Owner = Application.Current.MainWindow;
但是,这种方法会导致问题,如果你有三个窗口打开这样的:
MainWindow
|
-----> ChildWindow1
|
-----> ChildWindow2
然后设置ChildWindow2.Owner = Application.Current.MainWindow
将设置该窗口的所有者是其祖父窗口,而不是父窗口。
为了加快速度,我已经将它作为Visual Studio中的代码片段添加了。如果添加下列工具 - >代码段管理器 - >我的代码片断:
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2010/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>MVVM Set owner of page to be current active window</Title>
<Shortcut>owner</Shortcut>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
输入“主人”,双攻Tab键会自动添加“Application.CurrentWindows...
”部分为您服务。
我们可以从创建窗口的位置获取一些代码,该窗口将成为模式对话框? – user7116
您需要的是让窗口超出所有其他应用程序。我需要的是让窗口在应用程序中的任何其他窗口之上,例如对话窗口。对于我的要求,这两行:Owner = Application.Current.MainWindow;和ShowInTaskbar = false;效果很好。为你+1。 –