Eduardo Molteni
Eduardo Molteni

Reputation: 39413

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

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.

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.

What is the best way to do this in WPF?

Upvotes: 16

Views: 59638

Answers (4)

Dean Chalk
Dean Chalk

Reputation: 20461

Why not just use nested message pumps to create modal controls

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

Upvotes: 5

Brad Leach
Brad Leach

Reputation: 16997

This StackOverflow answer may 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.

Hope this helps!

Upvotes: 18

Caleb Vear
Caleb Vear

Reputation: 2647

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 IsOpen and when it is changed set the controls visibility to the appropriate value.

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.

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.

Upvotes: 3

dkretz
dkretz

Reputation: 37655

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.

Wikipedia has a good discussion.

Upvotes: 1

Related Questions