I am doing some experiments and I am trying to collect all elements which are placed under mouse pointer.
XAML test code
<StackPanel>
<Button
x:Name="HitButton"
Content="Test Button" />
<Button
x:Name="NotHitButton"
IsHitTestVisible="False"
Content="Test Button" />
</StackPanel>
To get coordinates of mouse pointer I use PointerMoved:
CoreWindow.GetForCurrentThread().PointerMoved += OnPointerMoved;
private void OnPointerMoved(CoreWindow sender, PointerEventArgs args)
{
Point point = args.CurrentPoint.Position;
IEnumerable<UIElement> elements = VisualTreeHelper.FindElementsInHostCoordinates(point, null, true);
...
}
This approach works, however, when a control has property IsHitTestVisible set up to false, FindElementsInHostCoordinates does not consider it even if I set up the third parameter includeAllElements of this method to true.
According to documentation I would expect if I set the parameter includeAllElements to true, it should find all elements, including those controls which have IsHitTestVisible set up to false.
includeAllElements [System.Boolean]
true to include all elements that intersect, including those elements considered to be invisible to hit testing. false to find only visible, hit-testable elements. The default is false.
Do I understand wrong way how FindElementsInHostCoordinates works? If so, is there any other way how to retrieve all controls at specific coordinates even if they have IsHitTestVisible set to false?
I'd think the key point of this question is what are the "elements considered to be invisible to hit testing".
I'd think when we set
IsHitTestVisibletofalse, the element is not considered to be invisible, it's just invisible. And according to Hit testing and input events, the element meets following criteria is hit-testable.So I think when a element's
Visibilityproperty value isCollapsedor itsBackgroundorFillproperty value isnullor itsIsEnabledproperty value isfalse, the element is considered to be invisible. And I did a simple test.When
includeAllElementsparameter is set tofalse, the output is likeAnd when
includeAllElementsparameter is set totrue, the output is likeSo it seems that
includeAllElementsparameter only takes effect when element'sBackgroundorFillproperty value isnull. Not sure if this is by design. Maybe only the elements whoseBackgroundorFillproperty value isnullis considered to be invisible.