Determining whether an object is visible and interactive

I'm looking for ways to effectively determine if a control is really visible and viewable. I mean, without checking the Visibility property of the object.

I can check RenderSize and it will be [0,0] if any of the parent elements are collapsed. So it's easy too. I can also cross the visual tree and see if the opacity of all elements is set to 1.

What I don't know is how to test these scenarios well:

  • An object is obstructed by another object . Obviously, you can use FindElementsInHostCoordinates () and perform calculations to find out how much these objects are obstructing, but this can be redundant. I can also take a “screenshot” of the object in question and “screenshots” of the entire page and check if the pixels match where my object should correspond to the actual pixels of the object. This also seems to be superfluous.
  • The object is hindered by a transparent object, which is still "swallowing" clicks (taps) . Workarounds for the first issue may still crash in this scenario.

Refresh (another scenario)

  • The object is outside the parent object / screen . Is there any other way to find out, besides a lot of calculations?

Any better ideas? Did I miss something?

Thanks!

+6
windows-phone-7 silverlight
source share
2 answers

You can programmatically test the Visiblity and HitTestVisible element, but outside of that, you cannot actually test if the click event is redirected to the element or swallowed by the elements on top of it - because blocking or redirecting clicks can occur in event handler methods that you simply can’t determine in a general way.

Take the following example:

  • You want to check if the ParentElement panel that has a ChildElement on it is ChildElement
  • ChildElement has an event handler attached to it that handles the click event, it is visible, and its HitTestVisible property is true.
  • Based on this, you still cannot decide whether the event will be sent to the ParentElement : in the ChildElement click event handler, it can set the event Handled to true, which would stop the ParentPanel, receiving it or it could leave it false, which would make the ParentPanel an event.

Thus, if there are special EventHandlers, you won’t be able to determine exactly whether the item is clickable or not. The most you can do is check the Visiblity and IsHitTestVisible on the child and parent elements and check where the children / parents are displayed relative to each other using TransformToVisual .

+3
source share

You are looking for VisualTreeHelper.FindElementsInHostCoordinates . The first item returned will be the item that gets the mouse click / tap on that pixel. Of course, as Gergeli said, you cannot say whether this element will ignore the click and pass it to you.

Here's an example function:

 // pass in a point and the UIElement that the point is relative to // (or null if the point is relative to the root visual) public static UIElement HitTest(Point p, UIElement relativeTo = null) { if (relativeTo != null) p = relativeTo.TransformToVisual(Application.Current.RootVisual) return VisualTreeHelper .FindElementsInHostCoordinates(p, Application.Current.RootVisual) .FirstOrDefault(); } 
0
source share

All Articles