问题
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