3

I'm trying to find UIElement from coordinate in my Silverlight for WP7 application.

Here is my XAML :

<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<Canvas x:Name="ContentPanel" Grid.Row="1">
    <Rectangle Canvas.Top="20" Canvas.Left="20" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="20" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="20" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />

    <Rectangle Canvas.Top="20" Canvas.Left="90" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="90" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="90" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />

    <Rectangle Canvas.Top="20" Canvas.Left="160" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="160" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="160" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />

    <Rectangle Canvas.Top="20" Canvas.Left="230" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="90" Canvas.Left="230" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
    <Rectangle Canvas.Top="160" Canvas.Left="230" Width="50" Height="50" Fill="Aqua" Tap="Rectangle_Tap" />
</Canvas>

and my code-behind :

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void Rectangle_Tap(object sender, GestureEventArgs e)
    {
        Rectangle r = (Rectangle)sender;

        double x = (double)r.GetValue(Canvas.LeftProperty);
        double y = (double)r.GetValue(Canvas.TopProperty);

        Debug.WriteLine(x + "," + y);

        var query = VisualTreeHelper.FindElementsInHostCoordinates(new Point(x - 70, y), ContentPanel).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x + 70, y), ContentPanel)).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x, y - 70), ContentPanel)).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x, y + 70), ContentPanel)).Union(
            VisualTreeHelper.FindElementsInHostCoordinates(new Point(x, y), ContentPanel));

        foreach (UIElement element in query.OfType<Rectangle>())
        {
            // never goes here
            ContentPanel.Children.Remove(element);
        }
    }
}

but the problem is that the method nevers return my Rectangle.

What's wrong with my code ?

Thanks in advance for any help. Best regards

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Tim
  • 1,749
  • 3
  • 24
  • 48

1 Answers1

3

I´m not sure if this returns the right value (for you, cause your question is not clear), but you should try the following

private void Rectangle_Tap(object sender, GestureEventArgs e)
{
    Point position = e.GetPosition(Application.Current.RootVisual);

    var query = VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X - 70, position.Y), Application.Current.RootVisual).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X + 70, position.Y), Application.Current.RootVisual)).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X, position.Y - 70), Application.Current.RootVisual)).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X, position.Y + 70), Application.Current.RootVisual)).Union(
        VisualTreeHelper.FindElementsInHostCoordinates(new Point(position.X, position.Y), Application.Current.RootVisual));

    foreach (UIElement element in query.OfType<Rectangle>())
    {
        // never goes here
        ContentPanel.Children.Remove(element);
    }
}

Sources i used:

VisualTreeHelper.FindElementsInHostCoordinates Method (Point, UIElement) GestureEventArgs.GetPosition Method

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • @anatoliiG Good to hear, that the solution works. Thanks for testing – Jehof Jun 25 '12 at 12:56
  • Great, this is working. Just need to replace Application.RootVisual with Application.Current.RootVisual. I think I was not in good refential. – Tim Jun 25 '12 at 13:32