Reputation: 771
For my Drag and Drop from ListView to Canvas, I want the objects to get dropped like I picked them up. For example:
I pick up an Item, and my mouse is 50 pixels lower, and 20 pixels right from the top left corner of the ListViewItem. Then I want the item, when I drop it, to be 50 pixels higher and 20 pixels left, so I get the same position relative to when I picked it up. This is my event that gets triggered, when I click onto an Item and start to drag:
void StackPanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
int index = GetCurrentIndex(e.GetPosition);
var ListIteminstance = ModuleListView.Items[index] as ListViewItem;
// Now you got the original object and then do what you want.
startPoint = e.GetPosition(ListIteminstance);
}
ListIteminstance
is null, cause it cannot convert the item to a ListViewItem. Cause of that, I only get the coordinates "0, 0" from the Item, when I drop it, it's from the top left corner, instead of the relative coordinates where I picked it up
GetCurrentIndex()
gets me the index of the Item I am clicking on. But it is not a ListViewItem, but the object of type "MyClass", that I store in the list. Now I'm wondering, how I can get the ListViewItem I am clicking on, so that ListIteminstance
is not null
Upvotes: 0
Views: 876
Reputation: 169200
Try this:
var dataObject = ModuleListView.Items[index];
var ListIteminstance = ModuleListView.ItemContainerGenerator.ContainerFromItem(dataObject)
as ListViewItem;
Upvotes: 1