单击Windows应用商店应用程序中的按钮后,应在按钮附近显示弹出控件.使用VerticalOffset和Horizo??ntalOffset属性(==相对于左上角的坐标)定位Popup是没有问题的,但是如何获得按钮相对于屏幕的位置来正确计算偏移?按...
单击Windows应用商店应用程序中的按钮后,应在按钮附近显示弹出控件.
使用VerticalOffset和Horizo??ntalOffset属性(==相对于左上角的坐标)定位Popup是没有问题的,但是如何获得按钮相对于屏幕的位置来正确计算偏移?
按钮位于页面上的某处,并且相对于屏幕没有固定位置(例如,当放置在滚动视图中时等).如何获得按钮的坐标?
解决方法:
您需要使用这些电话:UIElement.TransformToVisual和GeneralTransform.TransformPoint.
不幸的是,自从我这样做以来已经有一段时间了,所以我无法详细说明这些调用究竟是做什么的.
下面的代码显示按钮的左上角位置,并在那里打开弹出窗口.您可能希望根据需要调整定位.
private void btnClickMe_Click(object sender, RoutedEventArgs e)
{
Popup popUp = new Popup();
// TODO: Add your elements to the popup here...
// open the popup at the same coordinates as the button
var point = CalcOffsets((UIElement)sender);
popUp.HorizontalOffset = point.X;
popUp.VerticalOffset = point.Y;
popUp.IsOpen = true;
}
private Point CalcOffsets(UIElement elem)
{
// I don't recall the exact specifics on why these
// calls are needed - but this works.
var transform = elem.TransformToVisual(this);
Point point = transform.TransformPoint(new Point(0, 0));
return point;
}
沃梦达教程
本文标题为:c# – 如何相对于按钮在Windows应用商店应用中定位弹出控件?
猜你喜欢
- user32.dll 函数说明小结 2022-12-26
- Oracle中for循环的使用方法 2023-07-04
- Unity3D实现渐变颜色效果 2023-01-16
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- .NET CORE DI 依赖注入 2023-09-27
- 如何使用C# 捕获进程输出 2023-03-10
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- Unity Shader实现模糊效果 2023-04-27
- c# 模拟线性回归的示例 2023-03-14