tags:

views:

1132

answers:

4

I am dropping something in a ListView in WPF. I need to know the item in the (X,Y) position I am dropping. How can I do this?

+1  A: 

You want to use the GetItemAt function. You may also need to call the PointToClient function before the GetItemAt since you need to work with client coordinates.

arul
The WPF ListView doesn't have GetItemAt. I return to my original problem.
Mariano
I'm giving you an upvote because this answer came up when I was actually looking for a winforms related question. Thanks for the help even though you were off-topic.
Christopher Painter
A: 

The WPF ListView doesn't have GetItemAt. I return to my original problem.

Mariano
+1  A: 

Done! Thanks to this article http://www.codeproject.com/KB/WPF/WPF_Drag_And_Drop_Sample.aspx

private int GetCurrentIndex(GetPositionDelegate getPosition)
{
    int index = -1;
    for (int i = 0; i < clasesListView.Items.Count; ++i)
    {
        ListViewItem item = GetListViewItem(i);
        if (this.IsMouseOverTarget(item, getPosition))
        {
            index = i;
            break;
        }
    }
    return index;
}

private bool IsMouseOverTarget(Visual target, GetPositionDelegate getPosition)
{
    Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
    Point mousePos = getPosition((IInputElement)target);
    return bounds.Contains(mousePos);
}

delegate Point GetPositionDelegate(IInputElement element);

ListViewItem GetListViewItem(int index)
{
    if (clasesListView.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
        return null;

    return clasesListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
}
Mariano
+1  A: 

Hoops, sorry. This should work fine:

FrameworkElement element = (FrameworkElement)e.OriginalSource;

ListViewItem lvi = (ListViewItem)listView1.ItemContainerGenerator.ContainerFromItem(fe.DataContext);
arul