[英]How to position a popup control in a Windows Store App relative to a button?
單擊Windows應用商店應用程序中的按鈕后,應在按鈕附近顯示Popup
控件。
使用VerticalOffset
和HorizontalOffset
屬性(==相對於左上角的坐標)定位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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.