将弹出窗口绑定到另一个控件的相对屏幕位置 [英] Binding a Popup to another control's relative screen position

查看:144
本文介绍了将弹出窗口绑定到另一个控件的相对屏幕位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个XBAP,它带有一个复杂的Popup(画布Z索引为99,上面有一个网格...),我想附加"到打开它的按钮上,并在该按钮周围的任意位置跟随它出现在屏幕上.例如,如果按钮在ListBox或XamDataGrid中,则我希望弹出窗口跟随按钮滚动.如果它在扩展器下面,我希望它在扩展器强迫其移动时保持与按钮的连接,等等.

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.

有什么想法吗?

推荐答案

使用Popup时,在最初显示弹出窗口后,不会同时使用PlacementTargetCustomPopupPlacementCallback.因此,对这些属性的任何使用都将不允许弹出窗口在按钮移动时对其进行跟踪.

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:

  1. 将自定义Adorner附加到按钮,并将弹出窗口放入其中.缺点:Popup未连接到Button或周围的元素,因此它不会继承属性&来自他们的DataContext.

  1. 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.

将自定义Adorner附加到按钮.当按钮相对于AdornerLayer移动时,该装饰器将进行度量并安排呼叫,使您可以手动更新Popup位置.只要AdornerDecorator相对于Window不移动(例如,如果它是Window的直接子级),就可以通过监视Window大小的变化轻松地检测到AdornerLayer正在移动.缺点:代码复杂正确.

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.

根本不使用Popup.取而代之的是将按钮包装在<Grid>旁边的<Canvas>中,宽度和高度为零且位置期望.在<Canvas>内部,为弹出式窗口添加UserControl,并带有适当的ZIndex.它会超出Canvas的边缘,这在WPF中就可以了.除了使用Popup控件外,还可以控制UserControl的可见性.缺点:不能真正完全放在所有其他对象之上,不能延伸到窗口边缘之外(不过,对于XBAP来说可能不是问题).

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).

这篇关于将弹出窗口绑定到另一个控件的相对屏幕位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆