WPF:提高页面的模态弹出窗口的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/429200/
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-08 19:56:54  来源:igfitidea点击:

WPF: Best way to raise a popup window that is modal to the page?

wpfpopup

提问by Eduardo Molteni

I am building a WPF app using navigation style pages and not windows. I want to show a window inside a page, this window must be modal to the page, but allow the user to go to other page, and go back to the same page with the modal window in the same state.

我正在使用导航样式页面而不是窗口构建 WPF 应用程序。我想在页面内显示一个窗口,这个窗口必须是该页面的模态窗口,但允许用户转到其他页面,并返回到与模态窗口处于相同状态的同一页面。

I have tried with the WPF popup control but the problem is that the control hides everytime you navigate away from the page. I guess that I can write the code to show it again, but does not seams the right way.

我曾尝试使用 WPF 弹出控件,但问题是每次您离开页面时该控件都会隐藏。我想我可以编写代码来再次显示它,但没有以正确的方式接缝。

What is the best way to do this in WPF?

在 WPF 中执行此操作的最佳方法是什么?

回答by Brad Leach

This StackOverflow answermay help you on your way. I created some sample code that some other users have asked for. I have added this to a blog post here.

这个StackOverflow 答案可能会帮助你。我创建了一些其他用户要求的示例代码。我已将此添加到此处的博客文章中。

Hope this helps!

希望这可以帮助!

回答by Dean Chalk

Why not just use nested message pumps to create modal controls

为什么不直接使用嵌套消息泵来创建模态控件

http://www.deanchalk.com/wpf-modal-controls-via-dispatcherframe-nested-message-pumps/

http://www.deanchalk.com/wpf-modal-controls-via-dispatcherframe-nested-message-pumps/

回答by Caleb Vear

You could make a popup class that uses the adorner layer to put itself ontop of everything else.

您可以创建一个使用装饰层将自身置于其他所有内容之上的弹出类。

Make a base class for your popup that has a property called IsOpenand when it is changed set the controls visibility to the appropriate value.

为具有名为IsOpen的属性的弹出窗口创建基类,并在更改时将控件可见性设置为适当的值。

To stop the controls that are underneath your popup being clicked you can make the popup take of the full size of the page. You would have it mostly transparent except for the actuall middle where the popup. If you wanted it to be completly transparent execpt for the popup you would need to override the HitTestCore on your popup.

要停止单击弹出窗口下方的控件,您可以使弹出窗口占据页面的完整大小。除了弹出窗口的实际中间之外,您将使其大部分透明。如果您希望它对弹出窗口完全透明,则需要覆盖弹出窗口上的 HitTestCore。

Something like this:

像这样的东西:

protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
{
    // We want this control to behaive as a single rectangle and we don't much care
    // whether or it has a solid background.  So we override this method so we can have    // mouse over info for the entire panel regardless of background.

    // run the base hit test first, because if it finds something we don't want to overrule it.
    HitTestResult result = base.HitTestCore(hitTestParameters);


    // If we didn't get a hit generate a new hit test result, because HitTestCore is never called unless
    // the mouse is over the controls bounding rectangle.
    if (result == null)
        result = new PointHitTestResult(this, hitTestParameters.HitPoint);

    return result;
}

I hope this can point you in the right direction.

我希望这可以为您指明正确的方向。

回答by dkretz

Windows doesn't like you to do that - it's not a WPF thing. Use an overlying panel and use the visible or zorder property.

Windows 不喜欢你这样做 - 这不是 WPF 的事情。使用覆盖面板并使用可见或 zorder 属性。

Wikipediahas a good discussion.

维基百科有一个很好的讨论。