将 Popup 绑定到另一个控件的相对屏幕位置

发布于 2024-07-23 05:23:51 字数 190 浏览 3 评论 0原文

我正在编写一个带有复杂弹出窗口(画布 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-07-30 05:23:52

使用 Popup 时,在弹出窗口最初出现后,既不使用 PlacementTarget 也不使用 CustomPopupPlacementCallback。 因此,对这些属性的任何使用都不允许弹出窗口在按钮移动时跟踪按钮。

我想到了几种方法来实现您想要的:

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

  2. 将自定义Adorner附加到按钮。 当按钮相对于 AdornerLayer 移动时,此装饰器将获得测量和安排调用,从而允许您手动更新弹出窗口位置。 只要您的 AdornerDecorator 不相对于您的 Window 移动(例如,如果它是 Window 的直接子级),您就可以轻松检测到 AdornerLayer< /code> 通过监视窗口大小的变化来移动。 缺点:代码复杂且复杂 正确。

  3. 根本不要使用弹出窗口。 而是将按钮包装在 中,旁边是宽度和高度为零且位置理想的 。 在 内为弹出窗口添加具有适当 ZIndex 的 UserControl。 它将延伸超过 Canvas 的边缘,这在 WPF 中很好。 不使用 Popup 控件,只需控制 UserControl 的可见性。 缺点:不会真正完全位于所有其他对象之上,无法延伸到窗口边缘(不过,对于 XBAP 来说可能不是问题)。

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

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

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

贱贱哒 2024-07-30 05:23:52

我不确定它是否会为您自动更新,但是 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文