WPF Popup Alternative - wpf

I have a window with a popup that pops when an item in a listview is double clicked.
It centers to the main window and looks really nice floating there.
The problem is when the user moves the main window or selects another program, and the popup floats on top of other stuff.
I would like to have something like a popup, meaning that it floats on top of other elements in the window, but sticks with the main window when it moves (stays centered), and doesn't float on top of other programs.
Can I make a popup act like this, or is there a better way to do it?

Popups will not move while the window is resized or moved. Because, Popups/Context menus are not the part of Visual Tree. You have to use Adorner for this. I will suggest to read this four part series for a quick start on Adorner.

It's possible that an Adorner will fit your needs in this case better than a popup. Adorners can float above your window, too. There are a few differences, mainly that an adorner is bound to a UIElement (which include windows).

If you are willing to use a third-party/open source (MS-PL) option, the Extended WPF Toolkit has a ChildWindow control.
It's technically not a separate window, but it appears to be a separate window to the user.

I have not found a way to make Popups stop doing that in WPF
As an alternative, you can create a UserControl which acts like a Popup.
Usually I host the content section of the app along with the Popup within a Canvas control, and when IsPopupOpen gets changed to True I set the popup Visibility = Visible.

Related

Can a dock window dock anywhere or just on top, bottom, left, right?

I need use a lot of such windows in my UIs (WPF, WinForms)
say a label with text "Settings", When mouse hovers it, a window will show up and when mouse moves away, the window disappear. The label and the window should line up looks as if they were a tab control with the label being header, the window being tab item.
The label can be in anywhere of main UI.
I am thinking of dock window.
I just start so not sure if my question makes sense.
My understanding is I can use nested dock windows, like use a dock window inside a panel, the panel is one child of parent panel/window. but nested panels are not quite straight/easy. so I wonder if a dock window can dock anywhere? thanks
I think the best solution for you could be to use an existing library and don't reinvent the wheel. Please see THIS excellent docking library, possible it offers all requirements that you need. Its name is AvalonDock and it is used by several big projects. An example is SharpDevelop, this software use the AvalonDock library. Hope this could be helpful to you...

ToolTip versus Popup (WPF control)

What are the main differences between these WPF controls?
And when I should use a ToolTip instead of Popup?
A ToolTip is a small pop-up window that appears when a user pauses the mouse pointer over an element; the Popup control provides a way to display content in a separate window that floats over the current application window relative to a designated element or screen coordinate.
A Popup does not automatically open when the mouse pointer moves over its parent object. If you want a Popup to automatically open, use the ToolTip or ToolTipService class.
For more information see: ToolTip overview and Popup overview.
Previous tooltips in Winforms were overly restrictive in what you could display. This is why people rolled their own. WPF tooltips allow you to apply a templatize view with color, images, formatting, you name it. The days of rolling your own in that are mostly gone; mostly.

WPF Popup and WindowsFormsHost Problem

I am hosting windowsforms control in WPF popup. Problems below:
If i make StaysOpen=False i can't interact with winform control. StaysOpen to false is required because when clicked outsidet the Popup region, it should close.
if i make StaysOpen=True i can interact with winform control but when i click outside the area of popup, it is not getting closed.
I tried setting StaysOpen=true in MouseEnter of popup and StaysOpen=False in MouseLeave, but MouseLeave fires as and when mouse is over winform control resulting in unexpected behaviour.
I even tried IsMouseCaptureWithin property of popup and found it does not work with winforms (i guess its a bug in framework).
Another problem, i was trying to close popup when root main form (which is windows form) is deactivated (pressed Alt+Tab), but this event (deactivate) is fired even when i enter into one of the controls in windowshostControl in popup.
Desired Behaviour:
should be able to host and interact with winform control in wpf popup.
on clicking on outside the area of popup, popup should close.
Appreciate any inputs.
Thanks.
I've had many problems with the defacto-standard popups in WPF, because they are in fact a new window with their own handle. This means if you drag your application around the screen, the popup stays put (it doesn't move with your window). It also means your popup has some strange behaviors and doesn't interact with your application in ways other controls normally do.
I've created 2 decorator classes to address this problem:
PopupDecorator.cs and
TimeoutPopupDecorator.cs
It's pretty simple to use:
Add a namespace declaration for the new popup classes. i.e.
xmlns:dday_wpf="clr-namespace:DDay.WPF"
Surround the area you want the popup to be able to be displayed with the decorator. i.e.
<dday_wpf:PopupDecorator x:Name="popup">
<dday_wpf:PopupDecorator.Popup>
... contents of popup go here ...
</dday_wpf:PopupDecorator.Popup>
... contents of panel go here ...
</dday_wpf:PopupDecorator>
It works pretty much identically to a normal Popup from that moment on.
This may not solve all your problems, but hopefully it helps.
This sounds a bit like my problem launching a modeless winform control from a WPF form.
Check out my question Why is my WPF textbox "kinda" readonly?.
The just being, based on what Doug said about popups being a window with its own handle, makes this applicable.

Binding a Popup to another control's relative screen position

I'm writing an XBAP with a complex Popup (Canvas Z-index of 99 with a grid on it...) that I would like to "attach" to the button that opens it and follow that button around wherever it goes on the screen. For example, if the button is in a ListBox or an XamDataGrid I would like the popup to follow the button as it scrolls through. If it is beneath an Expander I want it to stay attached to the button when the expander forces it to move, etc.
Any Ideas?
When using a Popup, neither PlacementTarget nor CustomPopupPlacementCallback is used after the popup has originally appeared. So any use of these properties will not allow the popup to track the button as it moves.
Several ways occur to me of achieving what you desire:
Attach a custom Adorner to the button and put the popup inside it. Disadvantage: Popup is not connected to Button or surrounding elements, so it won't inherit properties & DataContext from them.
Attach a custom Adorner to the button. This adorner will get measure and arrange calls when the button moves relative to the AdornerLayer, allowing you to manually update the Popup position. As long as your AdornerDecorator doesn't move relative to your Window (eg if it is the direct child of the Window), you can easily detect the AdornerLayer being moved by monitoring changes to Window size. Disadvantage: Complex to code & get right.
Don't use a Popup at all. Instead wrap the button in a <Grid> alongside a <Canvas> with zero width and height and the desired position. Inside the <Canvas> add the UserControl for the popup with an appropriate ZIndex. It will extend past the edge f the Canvas, which is just fine in WPF. Instead of using a Popup control just control the visibility of the UserControl. Disadvantage: Will not really be totally on top of all other objects, can't extend off edge of window (may not be an issue for XBAP, though).
I'm not sure if it will auto-update for you or not, but the PlacementTarget property allows you to specify a control to position the popup relative to. If that doesn't work, then maybe CustomPopupPlacementCallback will do the trick?

Clip a wpf Popup at bounds of a main window

From what I understand, the popup exists within it's own visual tree. However, I've noticed a few properties, Clip and ClipToBounds. What I am wanting to do is Visually clip a popup at the right and bottom edges of a window regardless of the fact that the popup is independent of the bounds of the window. I'm not using XAML, but if somebody knows how to do it in XAML, then that's fine. I can get to the main window using System.Windows.Application.Current.MainWindow. Is it possible from this to get a value that I can use to clip the popup? I'm assuming that if there is a value that I can use, then I would be able to bind the clipping of the popup to that value. This is really not necessary since after the popup initially opens, if the window gets moved or resized, the popup closes. So I would really only need to clip the popup when it opens. The reason I would like to do this is because although I am using a popup, I don't want it to appear as a popup that exists outside of the window. FYI this is for a popup calendar for a custom datebox. Any ideas, as well as clarification of misconceptions that I may have, would be greatly appreciated.
Furthermore, the popup can be launched from a user control that is not directly on the Main Window. So in that case it would be easier to use a popup. As apposed to a UC inside the XAML
I know this is a year old post, but in case any others come here looking for answers... If you don't need the popup to be outside of your window, why use a popup at all? It'd be far easier to simply use a control in a canvas (for instance) and control it via its Visibility property. Then you'd automagically get your clipping.

Resources