4

I’ve met strange behaviour for WPF design-time in Visual Studio 2010: after an instance of the Popup class was created, and I switched the code tab in Visual Studio to a different file, the Popup still remains on the screen!

enter image description here

I have a piece of code, which allows to reproduce this, but I am not sure if I should paste it here (it's not so short), so maybe I'll just give a link to it: here.

infografnet
  • 3,749
  • 1
  • 35
  • 35

2 Answers2

2

For unknown reasons beyond mere mortals' comprehension, Microsoft has decided this is the default behavior of the Popup class in WPF. You have to implement the "hiding" logic yourself. I suggest handling the Window.LocationChanged, Window.Activated and Window.Deactivated events of the Window containing the Popup and close it yourself.

Edit: To clarify myself, the Window events you need to handle are the events of the window that contains the Popup's PlacementTarget element. Usually when you create a popup, you set it relative to some element contained in an application's Window (similar to how the tooltips work). If this is your case, then my solution is correct, but I forgot to mention this point about the PlacementTarget.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Thanks for an answer, but it seems, like there is no Parent of type Window (nor parent of parent) at design-time in WPF. Apparently the parents are instances of some internal classes like MS.Internal.Designer.ZoomableViewPresenter for example. But you pointed me in good direction (and for this I vote +1), so I will try to examine events of those instances. Thanks. – infografnet Nov 12 '12 at 22:52
  • Thanks for edit. I iterated in design-time all parents of my `PlacementTarget` (slider in my example, see [link](http://codeyourself.net/wpf-popup-permanently-displays-in-visual-studio-design-time/) ), but the problem is, that in design-time, Visual Studio is not creating application's `Window` (even if xaml defines exactly `Window` type), but instead of this, it's child is instantiated in MS.Internal.Designer.ZoomableViewPresenter (and other non-`Window` classes, which don't have suitable events). Of course in run-time it's different - there is `Window` as a parent, like it should be. – infografnet Nov 12 '12 at 23:38
1

In your code behind; you can simple check this boolean:

DesignerProperties.GetIsInDesignMode(this);

"this" represent the object containing the popup. For example the Window.

If true you can say:

myPopUp.IsOpen = false;

For Store Apps/WinRT:

Windows.ApplicationModel.DesignMode.DesignModeEnabled
Jonathan Alfaro
  • 4,013
  • 3
  • 29
  • 32