I am using the VisualTreeHelper
to perform a HitTest
on a button that looks like this...
<Button Width="100"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Purple">
Hello world
</Button>
However when I perform the actual HitTest as such:
HitTestResult result = VisualTreeHelper.HitTest(_ContentHolder, new Point(xTransform, yTransform));
if (result != null)
{
}
The returned object looks like this VisualHit = {System.Windows.Controls.Border}
which doesn't quite make sense to me.
Could anyone please provide any guidance as to what I might be doing wrong, how I would return the actual Button object (such that I can trigger any event handlers) and why I may be experiencing such behaviour?
If you look at the control template for the Button here you can see that it is composed almost entirely of a Border control. Since the Border is what actually took the hit, that is what is being returned. You should use a common helper function to walk up the visual tree to find the actual button.
Call this function, passing in the result of the hit test and specifying
Button
as the type should return the parent button that was hit.Now... With all that being said, you might be going about what you are trying to do the wrong way. Without more details about what you are doing and, more importantly why, it could be hard to guide you.