我正在构建面向Windows 8.1和Windows 10的Windows应用商店应用/通用应用,我希望能够在ListView之间拖放项目,并能够将项目放在ListView中的特定位置.我遇到的主要问题是我找不到一个好方法来确定项目被删除的列表索引.
我发现了一个示例(XAML ListView重新排序)但重要的区别是我列表中的项目具有可变高度,因此此示例项目用于推断索引的简单计算对我来说不起作用.
我能够获得ListView中放置项目的位置的x,y位置,但是我无法使用该位置来计算索引.我发现有人使用ListView.GetItemAt(x,y)或ListView.HitTest(x,y),但正如其他人发现的那样,这些方法似乎不存在于Windows Universal应用程序中.我也尝试过使用VisualTreeHelper.FindElementsInHostCoordinates(),但我要么没有正确使用它,要么我不理解它的用途,因为我不能让它返回结果.
这是我尝试过的一些示例代码:
private void ListView_OnDrop(object sender, DragEventArgs e)
{
var targetListView = (ListView)sender;
var positionRelativeToTarget = e.GetPosition(targetListView);
var rect = new Rect(positionRelativeToTarget, new Size(10, 15));
var elements = VisualTreeHelper.FindElementsInHostCoordinates(rect, targetListView);
// Trying to get the index in the list where the item was dropped
// 'elements' is always empty
}
Run Code Online (Sandbox Code Playgroud)
作为参考,我使用的是C#,XAML和Visual Studio 2013.
谢谢!