XAML ScrollViewer's child bring into view event

隐身守侯 提交于 2020-01-05 04:57:24

问题


I have a ScrollViewer, at the top area, there is a video player, and a ListView below the media player.

While scrolling up and down, when the media player bring into view, it starts to play. When scroll down till the media player disappears, the media player pauses.

So, how to do it? Thx.


回答1:


We can use ScrollViewer.ViewChanged event to know the user scrolling and zooming the ScrollViewer. But the ScrollViewer.ViewChanged do not raise when the layout changing.

So we can use LayoutUpdated event to do it.

We can get some property from ScrollViewer just as HorizontalOffset and VerticalOffset.

We can get the coordinates of the top left corner of a control relative to the previous control.

        var top = control.TransformToVisual(StackPanel).TransformPoint(new Point());

And then we can judge whether the element can be seen by the user through the intersection of the rectangles.

        var controlBounds = new Rect(top, control.DesiredSize);

        var viewBounds = new Rect(new Point(ScrollViewer.HorizontalOffset, ScrollViewer.VerticalOffset), new Size(ScrollViewer.ViewportWidth, ScrollViewer.ViewportHeight));

        if (RectIntersects(viewBounds, controlBounds))
        {

        }

We should write the RectIntersects the check two rectangles is intersectant.

    private static bool RectIntersects(Rect a, Rect b)
    {
        return !(b.Left > a.Right
            || b.Right < a.Left
            || b.Top > a.Bottom
            || b.Bottom < a.Top);
    }


来源:https://stackoverflow.com/questions/55862430/xaml-scrollviewers-child-bring-into-view-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!