amit kohan
amit kohan

Reputation: 1618

using a list in HitTest

This page on MSDN, represents an example of using HitTest which is clear in concept and ... but one thing I'm not getting is the list C# code points to as hitResultsList. I tried to declare it as List as:

List<myShapeClass> hitResultsList = new List<myShapeClass>();

however, I'm getting a type cast error. any help will be appreciated. Question is what kind of List should I use for HitTesting in general?

Code is here:

// Respond to the right mouse button down event by setting up a hit test results callback. 
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // Clear the contents of the list used for hit test results.
    hitResultsList.Clear();

    // Set up a callback to receive the hit test result enumeration.
    VisualTreeHelper.HitTest(myCanvas, null,
        new HitTestResultCallback(MyHitTestResult),
        new PointHitTestParameters(pt));

    // Perform actions on the hit test results list. 
    if (hitResultsList.Count > 0)
    {
        Console.WriteLine("Number of Visuals Hit: " + hitResultsList.Count);
    }
}

Thanks.

Upvotes: 0

Views: 502

Answers (1)

CodingGorilla
CodingGorilla

Reputation: 19842

The key is here, in the callback:

// Return the result of the hit test to the callback. 
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
    // Add the hit test result to the list that will be processed after the enumeration.
    hitResultsList.Add(result.VisualHit);

    // Set the behavior to return visuals at all z-order levels. 
    return HitTestResultBehavior.Continue;
}

Notice it is adding result.VisualHit, and result is a HitTestResult. So if you look up that member (http://msdn.microsoft.com/en-us/library/system.windows.media.hittestresult.visualhit.aspx) you'll see it's a DependencyObject.

So you want: List<DependencyObject>.

Upvotes: 2

Related Questions