uwp - UWP ListView Drop get index
问题描述
I have 2 ListViews, I can drag an item from ListView1 and drop it on to ListView2. If I drop the item between other items I want the item to be added in that same location.
So the question is: How do I get the position/index where I dropped my item?
I would expect to find the index somewhere in the DragEventArgs of the Drop event, but I can't find it anywhere:
private async void ListView2_Drop(object sender, DragEventArgs e)
{
}
解决方案
Anyway: I have tinkered around with this code and finally got it to work to my needs:
private async void MyListView_Drop(object sender, DragEventArgs e)
{
var scrollViewer = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(MyListView, 0), 0) as ScrollViewer;
var position = e.GetPosition((ListView)sender);
var positionY = scrollViewer.VerticalOffset + position.Y;
var index = GetItemIndex(positionY, MyListView);
// do something useful with the index...
}
int GetItemIndex(double positionY, ListView targetListView)
{
var index = 0;
double height = 0;
foreach (var item in targetListView.Items)
{
height += GetRowHeight(item, targetListView);
if (height > positionY) return index;
index++;
}
return index;
}
double GetRowHeight(object listItem, ListView targetListView)
{
var listItemContainer = targetListView.ContainerFromItem(listItem) as ListViewItem;
var height = listItemContainer.ActualHeight;
var marginTop = listItemContainer.Margin.Top;
return marginTop + height;
}
Thank you for pointing me in the right direction!
推荐阅读
- python - Pybluez 在不同设备上的不稳定行为 - Python
- crystal-reports - 水晶报告如果这个或那个然后这个2
- python - Python - 创建一个循环来更改 api 搜索结果并写入 JSON/CSV
- python - 我可以将 python 变量存储在另一个文件的类中吗?
- ios - 使用动态单元格高度保存 UITableview 的确切滚动位置
- laravel - 在 Schema 迁移中创建 FULLTEXT 字段
- sql - 使用 SQL Developer 探索 Oracle 数据库
- node.js - 无法使用 fetch() 从 React 客户端获取数据到 Nodejs 服务器
- django - django ConnectionResetError: [WinError 10054] 现有连接被远程主机强行关闭
- spring-boot-actuator - Spring Boot 健康检查时间表