Пример #1
0
        public void listView_Drop(object sender, DragEventArgs e)
        {
            if (this.ItemUnderDragCursor != null)
            {
                this.ItemUnderDragCursor = null;
            }

            e.Effects = DragDropEffects.None;

            if (!e.Data.GetDataPresent(typeof(ItemType)))
            {
                return;
            }

            // Get the data object which was dropped.
            ItemType data = e.Data.GetData(typeof(ItemType)) as ItemType;

            if (data == null)
            {
                return;
            }

            int newIndex = this.IndexUnderDragCursor;

            DataTable itemsSource = ((DataView)this.listView.ItemsSource).Table;

            if (this.ProcessDrop != null)
            {
                ProcessDropEventArgs <ItemType> args = new ProcessDropEventArgs <ItemType>(itemsSource, data, newIndex, e, e.AllowedEffects);
                this.ProcessDrop(this, args);
                e.Effects = DragDropEffects.Move;
            }
        }
Пример #2
0
        void listView_Drop(object sender, DragEventArgs e)
        {
            if (this.ItemUnderDragCursor != null)
            {
                this.ItemUnderDragCursor = null;
            }

            e.Effects = DragDropEffects.None;

            if (!e.Data.GetDataPresent(typeof(ItemType)))
            {
                return;
            }

            // Get the data object which was dropped.
            ItemType data = e.Data.GetData(typeof(ItemType)) as ItemType;

            if (data == null)
            {
                return;
            }

            // Get the ObservableCollection<ItemType> which contains the dropped data object.
            ObservableCollection <ItemType> itemsSource = this.listView.ItemsSource as ObservableCollection <ItemType>;

            if (itemsSource == null)
            {
                throw new Exception(
                          "A ListView managed by ListViewDragManager must have its ItemsSource set to an ObservableCollection<ItemType>.");
            }

            int oldIndex = itemsSource.IndexOf(data);
            int newIndex = this.IndexUnderDragCursor;

            if (newIndex < 0)
            {
                // The drag started somewhere else, and our ListView is empty
                // so make the new item the first in the list.
                if (itemsSource.Count == 0)
                {
                    newIndex = 0;
                }

                // The drag started somewhere else, but our ListView has items
                // so make the new item the last in the list.
                else if (oldIndex < 0)
                {
                    newIndex = itemsSource.Count;
                }

                // The user is trying to drop an item from our ListView into
                // our ListView, but the mouse is not over an item, so don't
                // let them drop it.
                else
                {
                    return;
                }
            }

            // Dropping an item back onto itself is not considered an actual 'drop'.
            if (oldIndex == newIndex)
            {
                return;
            }

            if (this.ProcessDrop != null)
            {
                // Let the client code process the drop.
                ProcessDropEventArgs <ItemType> args = new ProcessDropEventArgs <ItemType>(itemsSource, data, oldIndex, newIndex, e.AllowedEffects);
                this.ProcessDrop(this, args);
                e.Effects = args.Effects;
            }
            else
            {
                // Move the dragged data object from it's original index to the
                // new index (according to where the mouse cursor is).  If it was
                // not previously in the ListBox, then insert the item.
                if (oldIndex > -1)
                {
                    itemsSource.Move(oldIndex, newIndex);
                }
                else
                {
                    itemsSource.Insert(newIndex, data);
                }

                // Set the Effects property so that the call to DoDragDrop will return 'Move'.
                e.Effects = DragDropEffects.Move;
            }
        }
        void listView_Drop(object sender, DragEventArgs e)
        {
            if (this.ItemUnderDragCursor != null)
            {
                this.ItemUnderDragCursor = null;
            }

            e.Effects = DragDropEffects.Move;

            if (!e.Data.GetDataPresent(typeof(ItemType)))
            {
                return;
            }

            // Get the data object which was dropped.
            ItemType data = e.Data.GetData(typeof(ItemType)) as ItemType;

            if (data == null)
            {
                return;
            }

            // Get the ObservableCollection<ItemType> which contains the dropped data object.
            ObservableCollection <ItemType> itemsDestination = this.listViewDestination.ItemsSource as ObservableCollection <ItemType>;

            if (itemsDestination == null)
            {
                throw new Exception(
                          "A ListView managed by ListViewDragManager must have its ItemsSource set to an ObservableCollection<ItemType>.");
            }

            int oldIndex = itemsDestination.IndexOf(data);
            int newIndex = this.IndexUnderDragCursor;

            if (newIndex < 0)
            {
                // The drag started somewhere else, and our ListView is empty
                // so make the new item the first in the list.
                if (itemsDestination.Count == 0)
                {
                    newIndex = 0;
                }

                // The drag started somewhere else, but our ListView has items
                // so make the new item the last in the list.
                else if (oldIndex < 0)
                {
                    newIndex = itemsDestination.Count;
                }

                // The user is trying to drop an item from our ListView into
                // our ListView, but the mouse is not over an item, so don't
                // let them drop it.
                else
                {
                    return;
                }
            }

            // Dropping an item back onto itself is not considered an actual 'drop'.
            if (oldIndex == newIndex)
            {
                return;
            }

            if (this.ProcessDrop != null)
            {
                // Let the client code process the drop.
                ProcessDropEventArgs <ItemType> args = new ProcessDropEventArgs <ItemType>(itemsDestination, data, oldIndex, newIndex, e.AllowedEffects);
                this.ProcessDrop(this, args);
                e.Effects = args.Effects;
            }
            else
            {
                foreach (ListView lv in allListViews)
                {
                    ObservableCollection <InterfaceClass> itemsSource = lv.ItemsSource as ObservableCollection <InterfaceClass>;
                    InterfaceClass obj = null;
                    if (itemsSource != null)
                    {
                        foreach (InterfaceClass item in itemsSource)
                        {
                            obj = data as InterfaceClass;
                            if (item.Addres.Equals(obj.Addres))
                            {
                                break;
                            }
                        }
                    }
                    if (obj != null && itemsSource != null)
                    {
                        itemsSource.Remove(obj);
                    }
                }
                foreach (ListView lv in allListViews)
                {
                    ObservableCollection <FileExtensionClass> itemsSource = lv.ItemsSource as ObservableCollection <FileExtensionClass>;
                    FileExtensionClass obj = null;
                    if (itemsSource != null)
                    {
                        foreach (FileExtensionClass item in itemsSource)
                        {
                            obj = data as FileExtensionClass;
                            if (item.Extension.Equals(obj.Extension))
                            {
                                break;
                            }
                        }
                    }
                    if (obj != null && itemsSource != null)
                    {
                        itemsSource.Remove(obj);
                    }
                }

                itemsDestination.Insert(newIndex, data);

                // Set the Effects property so that the call to DoDragDrop will return 'Move'.
                e.Effects = DragDropEffects.Move;
            }
        }