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
2008-12-12 13:13:26
The WPF ListView doesn't have GetItemAt. I return to my original problem.
Mariano
2008-12-12 13:41:10
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
2010-07-30 17:13:18
A:
The WPF ListView doesn't have GetItemAt. I return to my original problem.
Mariano
2008-12-12 13:42:35
+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
2008-12-12 17:43:35
+1
A:
Hoops, sorry. This should work fine:
FrameworkElement element = (FrameworkElement)e.OriginalSource;
ListViewItem lvi = (ListViewItem)listView1.ItemContainerGenerator.ContainerFromItem(fe.DataContext);
arul
2008-12-12 17:45:48