How to know if an item is visible to user?

筅森魡賤 提交于 2019-12-11 21:41:35

问题


please look at this:

<Grid>
    <ScrollViewer>
        <Grid Background="Red" Width="50" Height="50" VerticalAlignment="Top" Margin="0,-50,0,0"/>
    </ScrollViewer>
</Grid>

here the red grid is not visible because of its margin. but when user pulls down, it will be visible on the screen.

How can I know when it is visible? thanks.

(It's a WP8 app, if that matters)


回答1:


This method might come handy for you.

private bool IsUserVisible(FrameworkElement element, FrameworkElement container)
{
    if (!element.IsVisible)
        return false;

    Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
    Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
    return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
}


来源:https://stackoverflow.com/questions/20038096/how-to-know-if-an-item-is-visible-to-user

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