Thursday, 3 July 2008

VisualTreeHelper.HitTest

VisualTreeHelper.HitTest is a cunning function available in Windows Presentation Foundation (WPF) classes that provides mouse hit testing for the given panel and point. It returns a result if the given point is inside a control in the given panel (grid, stack panel etc).
The basic form takes a panel and a point and returns a result, which if not null contains a VisualHit object which can be casted to whatever control was hit.
The reason I am telling you what you could find out on msdn is that when you pass a point and a panel into the function, it will assume the point is relative to the child coordinates of the panel and it will NOT take into account the fact that the panel is not necessarily located at 0,0 on its own parent panel. To ensure the point is passed in as child coordinates, inside your mouse function use the function:
MouseEventArgs.GetPosition()
and pass a reference to the panel on which you will be hit-testing rather than null or the parent panel which will give you the wrong child coordinates. For example:
<StackPanel MaxWidth="5000">
<Grid Name="JobsGrid" Canvas.Bottom="0" MaxWidth="5000">
</Grid>
<Grid Name="PeopleGrid" Canvas.Top="0" MaxWidth="5000">
</Grid>
</StackPanel>

protected void CanvasMouseMove(object sender, MouseEventArgs e)
{
HitTestResult Result = VisualTreeHelper.HitTest( JobsGrid, e.GetPosition(JobsGrid));
// etc
}
Post a Comment