How can I detect the controls under a point in UWP

Arushi Gupta 6 Reputation points
2021-11-08T12:52:44.517+00:00

Hi everyone,
I want to detect the controls under a point in UWP. whenever i tap on my application anywhere, the coordinates of the point will tell the controls under that point.

I used the below code in the code behind file of xaml-

private void Rectangle_Tapped(object sender, TappedRoutedEventArgs e)
{
var point = e.GetPosition(this);
var elements = VisualTreeHelper.FindElementsInHostCoordinates(point,grid);
}

the variable point is giving me the (x,y) coordinates, but the variable elements is giving the null value. what am i doing wrong, Please somebody tell me correct solution.

var elements = VisualTreeHelper.FindElementsInHostCoordinates(point,grid); - In this line the last word "grid" is the name property of "Grid" UIElement.

Below is my xaml code-
<Page
x:Class="NewTask.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:NewTask"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid Name="grid" Tapped="Grid_Tapped">
    <Grid Width="240" Height="100">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Button Grid.Column="0" Content="A" Click="Button_Click" HorizontalAlignment="Center"></Button>
        <Button Grid.Column="1" Content="B" HorizontalAlignment="Center"></Button>
        <Button Grid.Column="2" Content="C" HorizontalAlignment="Center"></Button>
        <Rectangle Name="rectangle"  Grid.ColumnSpan="3" Opacity="0.5" Fill="Transparent" Stroke="White" Tapped="Rectangle_Tapped"></Rectangle>
    </Grid>
</Grid>

</Page>

Below is my code behind file of xaml -

public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
//UIElement grid = new Grid();
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(BlankPage1));
}

    private void Rectangle_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var point = e.GetPosition(this);
        var elements = VisualTreeHelper.FindElementsInHostCoordinates(point,grid);
    }

    private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var point = e.GetPosition(this);
    }
}
Universal Windows Platform (UWP)
{count} vote

2 answers

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points
    2021-11-09T02:00:25.71+00:00

    Hello,
    Welcome to Microsoft Q&A!

    he variable point is giving me the (x,y) coordinates, but the variable elements is giving the null value.

    Derive from my testing, it could work well, and we can get a list of elements where in current point. you can use foreach to get each element, for more please refer the following code.

    private void Rectangle_Tapped(object sender, TappedRoutedEventArgs e)  
    {  
        var point = e.GetPosition(this);  
        var elements = VisualTreeHelper.FindElementsInHostCoordinates(point, grid, false);  
        foreach (var item in elements)  
        {  
            System.Diagnostics.Debug.WriteLine(item.GetType());  
        }  
    }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Nora Ahmed 6 Reputation points
    2022-06-16T11:07:37.223+00:00

    conversations, and other sounds (previously called "Sound Recorder"). Press the big Record button (1), add tags (3) to mark important moments during recording or…

    0 comments No comments