Mr D
Mr D

Reputation: 5

UWP Position absolute popup

I have popup. When click button in GridView, popup will show. I set VerticalOffset and HorizontalOffset for popup. But i have problem, when i scroll GridView popup not moving. I can set popup absolute?

Upvotes: 0

Views: 327

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39072

As the documentation says, the Offset properties set the position relative to the application window:

Gets or sets the distance between the left side of the application window and the left side of the popup.

This means, that the position is set in absolute relative to the window and will not update automatically when the GridView is scrolled. Instead, you will have to update it manually by observing the scroll viewer events. First use the VisualTreeHelper to find ScrollView inside the GridView:

public static ScrollViewer FindScrollViewer(DependencyObject d)
{
    if (d is ScrollViewer) return d as ScrollViewer;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)
    {
        var child = VisualTreeHelper.GetChild(d, i);

        var result = FindScrollViewer(child);
        if (result != null) return result;
    }
    return null;
}

You can use this helper method like this:

var scrollViewer = FindScrollViewer(MyGridView);

And now attach the scrollViewer.ViewChanged or scrollViewer.ViewChanging event and update the popup position as you see fit.

Upvotes: 1

Related Questions