簡體   English   中英

如何相對於按鈕在Windows應用商店應用中定位彈出控件?

[英]How to position a popup control in a Windows Store App relative to a button?

單擊Windows應用商店應用程序中的按鈕后,應在按鈕附近顯示Popup控件。

使用VerticalOffsetHorizontalOffset屬性(==相對於左上角的坐標)定位Popup是沒有問題的,但是如何獲得按鈕相對於屏幕的位置來正確計算偏移?

按鈕位於頁面上的某個位置,並且相對於屏幕沒有固定位置(例如,當放置在滾動視圖中時等)。 如何獲得按鈕的坐標?

您需要使用以下調用: UIElement.TransformToVisualGeneralTransform.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM