silverlight - 列表框拖动重新排序: Index of the dropped item

标签 silverlight drag-and-drop listbox

我使用的是包裹在ListBoxDragDropTarget(来自Silverlight 工具包)内的Listbox。 用户可以手动重新排序此ListBox。但是,最后一项必须始终位于 ListBox 的底部,并且根本无法移动。我找到了一种方法来取消最后一个项目的移动,但是如果我将另一个项目拖放到最后一个项目的下面,它就会被移动。

我可以在 ListBox 的 Drop 事件中使用以下代码找到拖动项目的索引:

object data = e.Data.GetData(e.Data.GetFormats()[0]);

ItemDragEventArgs dragEventArgs = data as ItemDragEventArgs;
SelectionCollection selectionCollection = dragEventArgs.Data as SelectionCollection;
if (selectionCollection != null)
{
    MyClass cw = selectionCollection[0].Item as MyClass;
    int idx = selectionCollection[0].Index.Value;   
}

但是,这仅给我拖动操作之前的索引。

我的问题如下:有没有办法知道删除的项目的new索引?这样,如果索引 = 列表的最后一个位置,我可以将其移动到最后一个位置。

提前致谢!

最佳答案

好的,我找到了一种方法来做到这一点。我绑定(bind)了 ListBoxDragDropTargetItemDragCompleted 事件,并执行了以下操作:

private void dropTarget1_ItemDragCompleted(object sender, ItemDragEventArgs e)
{
    var tmp = (e.DragSource as ListBox).ItemsSource.Cast<MyClass>().ToList();

    SelectionCollection selectionCollection = e.Data as SelectionCollection;
    if (selectionCollection != null)
    {
        MyClass cw = selectionCollection[0].Item as MyClass;

        int idx = tmp.IndexOf(cw);
        if (idx == tmp.Count - 1)
        {
            tmp.Remove(cw);
            tmp.Insert(tmp.Count - 1, cw);

            MyListBox.ItemsSource = new ObservableCollection<MyClass>(tmp);
        }
}

}

由于 DragSource 代表列表框,因此具有新的项目“排列”,因此我可以检查该项目现在是否位于末尾,并在这种情况下移动它。

剩下的唯一问题是,由于项目被放下然后移动,它会导致屏幕闪烁。

我仍然愿意接受任何其他(最佳)建议。

关于silverlight - 列表框拖动重新排序: Index of the dropped item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4851541/

相关文章:

c# - {Binding} 的代码隐藏等效项是什么?

wpf - Silverlight MVVM 框架

silverlight - Windows Phone 7 中是否有可用的颜色选择器控件?

vb.net - 从 Silverlight 4 调用 WCF 服务需要指导

带有拖放功能的 jQuery 布局

wpf - 将 ListBox 中的多个项目复制到剪贴板

c# - Silverlight 工具包——拦截拖动启动

Android 在 Canvas 中拖放/旋转位图

wpf - ListBox 总是自动选择第一项

css - GWT 列表框样式