yllams
yllams

Reputation: 13

Hit Testing ... is it possible to test items in a listbox or other items control

I'm trying to hit test items within a listbox but to no avail. Is it possible? The VisualTreeHelper shows the x,y coordinates returning 0,0 for each and every item within which would seem to rule hit testing out. Does anyone know of a solution or workaround?

Upvotes: 0

Views: 2135

Answers (2)

slayernoah
slayernoah

Reputation: 4472

Try this:

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
    var hti = listBox1.IndexFromPoint(e.X, e.Y);
    if (hti==-1)
    {
        //user didn't click on an item
    }
    else
    {
        //user clicked on an item
        if (hti==listBoxFolder.SelectedIndex)
        {
            //user clicked on the item that was already selected
        }
        else
        {
            //user clicked on an item that was not already selected
        }
    }
}

This assumes that listbox1.SelectionMode is set to SelectionMode.One (which is the default setting).

Upvotes: 0

Kent Boogaart
Kent Boogaart

Reputation: 178630

What have you tried? Have you tried the UIElement.InputHitTest method? The VisualTreeHelper.HitTest method? Have you read through this?

Based on your question I guess you're calling VisualTreeHelper.GetOffset? If so, the offset is relative to the parent, which may very well be 0,0.

You need to give us more information with which to help you.

Upvotes: 2

Related Questions