Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a WPF popup that is an extended popup so I can do some custom behavior etc on. There is one behavior that I can almost almost get from a straight up property on the PopUp. I want StaysOpen="false" to be the behavior I get, except when the loss of focus comes from a save dialog instantiated by a child element of said popup. The default behavior causes the dialog to instantly close because its parent element is closed when it steals focus from that parent element.

Any ideas how I can do this? I have tried different events like HasFocusChanged, but I can't find one that works like I would need to control this behavior more specifically.
Posted

1 solution

Unfortunately, the solution I finally worked out is not as clean as I would like. I like to work out so I extend a control in a way that isn't specifically tied to my given solution or project. This one is, but it
would be easy to use the control in other projects and just modify a few spots.

My extended pop up class (I call anchored pop up because it stays in the same relative spot to the other controls when a window is re sized or moved) is used deep inside control templates where it wouldn't be realistic to have it be aware of or connected the commands and actions that matter for this. As a result, I needed something that would be achievable without any knowledge or programmatic access to the ui controls that live with the popup. I needed to be able to extend the popup class to make it happen. Unfortunately the events available in the popup class are worthless for this. Lost focus calls when you click on child elements of the pop up, and yet the senders etc never think they are ancestors of the popup etc.

In my project, I have a singleton instance that is kind of an application manager. My viewmodels will invoke things like showing modals through that manager. The idea is that I can unit test my viewmodels with scaffolding for things like modal results etc. It turned out to also give me the path to my answer.

I added events to my singleton manager to notify when a modal is about to open, and when a modal has just closed. Then in the popup class I have a property that lets me determine if a given popup listens for modal notifications.

When the popup opens, I subscribe to the two events. When it closes I unsubscribe. In the handler for the modal about to open event I set StaysOpen to true, but first save the original StaysOpen value. In the event for when modals close, I restore the stays open value to the preserved value.

That lets me have stays open false, but still have the ability to invoke a browse modal from a child of the popup.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900