在Silverlight中获取拖放索引 [英] Get drop index in Silverlight drag / drop

查看:74
本文介绍了在Silverlight中获取拖放索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文章显示了如何在放置事件上实施复制操作。我想做同样的事情,但我希望我放置的项目根据其在UI上的放置位置出现在集合中。因此,当ObservableCollection更改时,我需要像在NotifyCollectionChangedEventArgs上一样的StartIndex。在本文中,您将看到最终得到一个SelectionCollection对象,该对象的项目具有Index属性。但是不幸的是,这是源集合(被选择的地方)的索引,而不是目标集合(被删除的地方)的索引。

This article shows how to implement a copy operation on a drop event. I'd like to do the same but I want my dropped item to appear in the collection according to where it was placed on the UI. So I need the StartIndex much like on a NotifyCollectionChangedEventArgs when an ObservableCollection changes. In the article you'll see that eventually you get a SelectionCollection object whose items have an Index property. But unfortunately this is the index of the source collection (where it was picked) and not the destination collection (where it was dropped).

推荐答案

好的,这很丑陋,但是我没有找到另一种方法,不是靠我自己,也不是在网上搜索答案。一定是在Microsoft的另一个截止日期之前,它阻止了相当明显的功能的包含。

Ok, this is quite ugly, but I didn't find another way, not by myself and also not by searching the net for answers. Must have been another deadline at Microsoft that prevented the rather obvious functionality to be included...

基本上,下面的方法手动完成所有操作,获取放置位置并进行检查用作索引引用的列表框项目。

Basically the method below does everything manually, getting the drop location and checking it for listbox items to use as index references.

private void ListBoxDragDropTarget_Drop(object sender, Microsoft.Windows.DragEventArgs e)
{
    // only valid for copying
    if (e.Effects.HasFlag(DragDropEffects.Copy))
    {
        SelectionCollection selections = ((ItemDragEventArgs)e.Data.GetData("System.Windows.Controls.ItemDragEventArgs")).Data as SelectionCollection;
        int? index = null;

        if (selections != null)
        {
            Point p1 = e.GetPosition(this.LayoutRoot); // get drop position relative to layout root
            var elements = VisualTreeHelper.FindElementsInHostCoordinates(p1, this.LayoutRoot); // get ui elements at drop location

            foreach (var dataItem in this.lbxConfiguration.Items) // iteration over data items
            {
                // get listbox item from data item
                ListBoxItem lbxItem = this.lbxConfiguration.ItemContainerGenerator.ContainerFromItem(dataItem) as ListBoxItem;

                // find listbox item that contains drop location
                if (elements.Contains(lbxItem))
                {
                    Point p2 = e.GetPosition(lbxItem); // get drop position relative to listbox item
                    index = this.lbxConfiguration.Items.IndexOf(dataItem); // new item will be inserted immediately before listbox item
                    if (p2.Y > lbxItem.ActualHeight / 2)
                        index += 1; // new item will be inserted after listbox item (drop location was in bottom half of listbox item)

                    break;
                }
            }

            if (index != null)
            {
                foreach (var selection in selections)
                {
                    // adding a new item to the listbox - adjust this to your model
                    (lbxConfiguration.ItemsSource as IList<ViewItem>).Insert((int)index, (selection.Item as ViewItem).Clone());
                }
            }
        }
    }
}

这篇关于在Silverlight中获取拖放索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆