How to place pop-up control in a Windows Store app in relation to a button?

After clicking the button in the Windows Store app, a control will be displayed next to the button Popup.

Positioning Popupusing its properties VerticalOffsetand HorizontalOffset(== coordinates relative to the upper left corner) is not a problem, but how can I get the position of the button relative to the screen to calculate the offsets correctly?

The button is located somewhere on the page and does not have a fixed position relative to the screen (for example, when placed in scroll mode, etc.). How to get the coordinates of the buttons?

+4
source share
1

: 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;
}
+5

Source: https://habr.com/ru/post/1529181/


All Articles