我可以在代码隐藏中判断弹出窗口是否已相对于其放置目标移动吗?
我正在开发一个 WPF 项目,其中有一个缩略图网格(每个缩略图都是一个 SurfaceButton),单击每个缩略图会显示其自己的弹出菜单。在每种情况下,弹出菜单都将其“同级”照片缩略图作为其放置目标。缩略图和弹出窗口在网格中并排放置。
我设计的弹出窗口是为了模拟您在 iOS 中找到的那种弹出可滚动菜单,并带有一个小三角形箭头,该箭头指向与弹出窗口相关的缩略图(弹出窗口出现在缩略图下方)。
但是,令我惊喜的是,如果您单击屏幕底部的缩略图,WPF 会将弹出窗口移动到缩略图上方,这样它就不会离开屏幕。
这很棒,但是如何比较两个元素的位置并相应地移动箭头(或隐藏它并显示另一个元素),以便缩略图上方的弹出窗口指向它?
希望这是有道理的!
我尝试过 VisualTreeHelper.GetOffset,但它只返回距元素父级的偏移量,并且这些似乎始终是 0,0 或 1,1。我也尝试过 UIElement.PointToScreen,但是当弹出窗口移动时,从两个元素返回的数字似乎没有变化。
我确信我缺少一个简单的解决方案。
I'm working on a WPF project where I have a grid of thumbnails (each thumbnail is a SurfaceButton), and clicking on each thumbnail shows its own popup menu. The popup menu in each case has its 'sibling' photo thumbnail as its placement target. The thumbnail and popup sit alongside each other in a Grid.
I've designed the popup to emulate the kind of popout scrollable menu that you'd find in iOS, complete with a little triangle arrow that points up to the thumbnail the popup relates to (with the popup appearing below the thumbnail).
But, much to my pleasant surprise, if you click on a thumbnail that's at the bottom of the screen, WPF moves the popup above the thumbnail so that it's not off-screen.
This is great, but how can I compare the positions of the two elements and move the arrow accordingly (or hide it and show another), so that the popup above the thumbnail is pointing down to it?
Hope that makes some sense!
I've tried VisualTreeHelper.GetOffset, but that only returns the offset from the element's parent, and these seem to always be 0,0 or 1,1. I've also tried UIElement.PointToScreen, but the numbers I get back from the two elements don't seem to vary when the popup moves.
I'm sure there's a simple solution that I'm missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像下面一样获取弹出窗口的实际位置,
弹出窗口没有自己的大小或位置,因此您必须仅在显示弹出窗口时捕获子窗口的位置。
https://learn.microsoft.com/en-我们/dotnet/framework/wpf/controls/popup-overview
You can get your popup's actual placement like below
Popup doesn't have it's own size or position, so you have to catch the child's position only when popup is shown.
https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/popup-overview