将 Popup 绑定到另一个控件的相对屏幕位置
我正在编写一个带有复杂弹出窗口(画布 Z 索引为 99,上面有网格...)的 XBAP,我想将其“附加”到打开它的按钮,并跟随该按钮,无论它在哪里屏幕。 例如,如果按钮位于 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.
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Popup
时,在弹出窗口最初出现后,既不使用PlacementTarget
也不使用CustomPopupPlacementCallback
。 因此,对这些属性的任何使用都不允许弹出窗口在按钮移动时跟踪按钮。我想到了几种方法来实现您想要的:
将自定义
Adorner
附加到按钮并将弹出窗口放入其中。 缺点:Popup 没有连接到 Button 或周围的元素,因此它不会继承属性和属性。 来自它们的 DataContext。将自定义
Adorner
附加到按钮。 当按钮相对于 AdornerLayer 移动时,此装饰器将获得测量和安排调用,从而允许您手动更新弹出窗口位置。 只要您的AdornerDecorator
不相对于您的Window
移动(例如,如果它是 Window 的直接子级),您就可以轻松检测到AdornerLayer< /code> 通过监视窗口大小的变化来移动。 缺点:代码复杂且复杂 正确。
根本不要使用
弹出窗口
。 而是将按钮包装在
中,旁边是宽度和高度为零且位置理想的。 在
内为弹出窗口添加具有适当 ZIndex 的
UserControl
。 它将延伸超过 Canvas 的边缘,这在 WPF 中很好。 不使用Popup
控件,只需控制UserControl
的可见性。 缺点:不会真正完全位于所有其他对象之上,无法延伸到窗口边缘(不过,对于 XBAP 来说可能不是问题)。When using a
Popup
, neitherPlacementTarget
norCustomPopupPlacementCallback
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 theAdornerLayer
, allowing you to manually update thePopup
position. As long as yourAdornerDecorator
doesn't move relative to yourWindow
(eg if it is the direct child of the Window), you can easily detect theAdornerLayer
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 theUserControl
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 aPopup
control just control the visibility of theUserControl
. 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).我不确定它是否会为您自动更新,但是 PlacementTarget 属性允许您指定一个控件来相对定位弹出窗口。 如果这不起作用,那么也许 CustomPopupPlacementCallback 可以解决问题吗?
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?