Most of the time, when you are creating games, you will have to find out all the UIElemetn in a specific coordinate. (Or we can this hittest). In such circumstance, you will need to use the method VisualTreeHelper.FindElementsInHostCoordinates. Please view the C# coding for more details.
Get all the UIElement in a specific coordinate:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace HitTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
App.Current.RootVisual.MouseLeftButtonDown += new MouseButtonEventHandler(RootVisual_MouseLeftButtonDown);
}
void RootVisual_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
List<uielement> list = VisualTreeHelper.FindElementsInHostCoordinates
(e.GetPosition(null), LayoutRoot as UIElement) as List<uielement>;
var selectedControls = from control in list
where control is Rectangle
select control as Rectangle;
if (selectedControls.Count() > 0)
{
foreach (var selectedControl in selectedControls)
{
// do your opreation here
}
}
}
}
}
Website: http://www.shinedraw.com






