简体   繁体   中英

How to check if a UIElement is visible in Silverlight?

How can I check if any given UIElement is currently visible on the UI?

There is the UIElement.Visibility property, but this is set by the progammer to indicate that the element should be hidden or visible.

I already check if the element is in the VisualTree.

All this does not help if there is another element on top that overlaps it.

WPF has a property UIElement.IsVisible that seems to do the job, but this is missing in Silverlight.

Any ideas?

Thanks

You can do some code to test the Visiblity and the HitTestVisible property of an element.

EDIT:

Try to do something like mentioned here, Silverlight - Determine if a UIElement is visible on screen

There is a way to check the "render visibility" (although it cannot check for overlapping elements):

Elements do have the Unloaded event. It is raised whenever changes to the VisualTree happen that result in the Element being part of a VisualTree branch that is not currently rendered. But luckily you don't have to listen for those events because only loaded elements do have decendants or a parent in the VisualTree. And with the help of a nice little ExtensionMethod you can check whether an element is loaded or not at any given point in time:

public static class FrameworkElementExtensions
{
    public static bool IsLoaded(this FrameworkElement element)
    {
        return element.GetVisualChildren().Any();

        //I'm not sure if this alternative is better:
        //return System.Windows.Media.VisualTreeHelper.GetParent(element)!= null;
        //or
        //return element.GetVisualParent() != null;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM