在我的 MVVM WPF 应用程序中,拖放是在两个 ItemsControl 之间实现的。DragOver 和 Drop 方法在 ItemCollectionViewModel 中实现。ItemCollection 中的 Item 是 EquipmentViewModel 模型类。
在 Drop Method 中,我正在检查 targetItem 如下情况 1:当我在其他设备上放置时,目标 TargetItem 是 Equipment,并且替换逻辑工作正常。
if (dropInfo.TargetItem is EquipmentViewModel targetEquipment)
{
// Equipment Replacement logic
}
案例 2:当我在项目之间插入时,dropInfo.TargetItem 是 ItemCollectionViewModel。现在,我无法获取目标项目集合的插入索引。它总是在最后添加。
if (dropInfo.TargetItem is ItemCollectionViewModel targetItemCollection)
{
// insert item in the targetItemCollection
}
如何在案例 2 中获取插入索引,以便可以将项目插入到丢弃的位置。