简体   繁体   English

WPF-将窗口移到最前面

[英]WPF - Bring Window to Front

I have a WPF-Window which I don't close. 我有一个没有关闭的WPF窗口。 Instead I do Hide() and Show() it. 相反,我做了Hide()和Show()。 Now, when I doubleclick in my MainWindow in a Grid on a Record, which will Trigger to Show() the Window, the Window will always be shown behind the MainWindow. 现在,当我在Record的网格中的MainWindow中双击时,它将触发Show()窗口,该窗口将始终显示在MainWindow的后面。 I have tried the fallowing, but with no success: 我尝试过休闲,但没有成功:

view.Show();
view.Activate();
view.Topmost = true;
view.Topmost = false;
view.Focus();       

Is there another way which I can use to bring the window absolut to the front? 还有另一种方法可以将绝对式窗户放到前面吗? I cannot set the MainWindow as the Owner. 我不能将MainWindow设置为Owner。

Window.Activate is the way to go (If you dont want to set to owner). Window.Activate是一种解决方法(如果您不想设置为所有者)。 If this does not work (as you describe), there is an error at another location. 如果这不起作用(如您所述),则在另一个位置有错误。 Maybe your MainWindow has TopMost set to true ? 也许您的MainWindow已将TopMost设置为true Or you have a deferred call that focuses your main window or a control within? 还是您有一个将您的主窗口或控件集中在其中的延迟调用?

Calling ShowDialog() as proposed in another answer is not an option unless you want that the whole app is blocked until the modal opened window is closed. 除非您希望整个应用程序都被阻止,直到关闭模式打开的窗口,否则无法调用另一个答案中建议的ShowDialog()。

There is an error in the Win32-Api that influences also the window-management if WPF, but the description of your problem does not sound like this. 如果使用WPF,Win32-Api中有一个错误也会影响窗口管理,但是您对问题的描述听起来不是这样。

Additionally here a hack, but I hope that you don't need it: 另外这里有一个hack,但我希望您不需要它:

Dispatcher.BeginInvoke(new Action(delegate {       
        view.Activate();
        }), System.Windows.Threading.DispatcherPriority.ContextIdle, null);

ShowDialog代替Show将使窗口始终显示在顶部。

myWindow.WindowState = WindowState.Normal;

那对我有用。

I had the same problem - I was showing a window with Owner set to NULL from a MouseDoubleClick event. 我遇到了同样的问题-我正在显示一个窗口,该窗口的MouseDoubleClick事件中的Owner设置为NULL。 I realised (eventually) that I needed to set: 我意识到(最终)我需要设置:

e.Handled = true

before my event code completed. 在我的活动代码完成之前。 The following Microsoft document outlines that you may want to mark an event as handled when it responds in a "significant and relatively complete way": 下面的Microsoft文档概述了当事件以“重要且相对完整的方式”响应时,您可能希望将事件标记为已处理:

http://msdn.microsoft.com/en-us/library/ms747183.aspx http://msdn.microsoft.com/en-us/library/ms747183.aspx

This is subjective, but in my case it was preventing the window I just opened from being visible to the user. 这是主观的,但就我而言,这阻止了我刚刚打开的窗口对用户可见。

I went through a similar issue and found a solution using a combination of the other answers. 我遇到了类似的问题,并结合了其他答案找到了解决方案。 Once the window is hidden I put it in the foreground with the following code : 一旦窗口被隐藏,我将使用以下代码将其放在前台:

    view.WindowState = WindowState.Normal;
    view.Activate();

Note : If the window was maximised before hiding, that code will make it come back as maximised 注意:如果窗口在隐藏之前已最大化,则该代码将使其最大化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM