Click here to Skip to main content
15,896,330 members
Articles / Desktop Programming / WPF

Drag and Drop in WPF - Part II

Rate me:
Please Sign up or sign in to vote.
4.94/5 (41 votes)
11 Nov 2009BSD4 min read 259.6K   11.4K   70  
An article showing how to add drag and drop to a WPF application using the GongSolutions.Wpf.DragDrop library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using GongSolutions.Wpf.DragDrop;
using System.Windows;
using System.Collections;

namespace SchoolsExample
{
    class MainViewModel : IDropTarget
    {
        public MainViewModel()
        {
            ObservableCollection<SchoolViewModel> schools = new ObservableCollection<SchoolViewModel>();

            schools.Add(new SchoolViewModel 
            { 
                Name = "Bloomfield School",
                Pupils = new ObservableCollection<PupilViewModel>
                {
                    new PupilViewModel { FullName = "Adam James" },
                    new PupilViewModel { FullName = "Sophie Johnston" },
                    new PupilViewModel { FullName = "Kevin Sandler" },
                    new PupilViewModel { FullName = "Oscar Peterson" }
                }
            });

            schools.Add(new SchoolViewModel 
            { 
                Name = "Redacre School",
                Pupils = new ObservableCollection<PupilViewModel>
                {
                    new PupilViewModel { FullName = "Tom Jefferson" },
                    new PupilViewModel { FullName = "Tony Potts" }
                }
            });

            schools.Add(new SchoolViewModel
            {
                Name = "Top Valley School",
                Pupils = new ObservableCollection<PupilViewModel>
                {
                    new PupilViewModel { FullName = "Alex Thompson" },
                    new PupilViewModel { FullName = "Tabitha Smith" },
                    new PupilViewModel { FullName = "Carl Pederson" },
                    new PupilViewModel { FullName = "Sarah Jones" },
                    new PupilViewModel { FullName = "Paul Lowcroft" }
                }
            });

            Schools = CollectionViewSource.GetDefaultView(schools);
        }

        void IDropTarget.DragOver(DropInfo dropInfo)
        {
            if (dropInfo.Data is PupilViewModel && dropInfo.TargetItem is SchoolViewModel)
            {
                dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
                dropInfo.Effects = DragDropEffects.Move;
            }
        }

        void IDropTarget.Drop(DropInfo dropInfo)
        {
            SchoolViewModel school = (SchoolViewModel)dropInfo.TargetItem;
            PupilViewModel pupil = (PupilViewModel)dropInfo.Data;
            school.Pupils.Add(pupil);
            ((IList)dropInfo.DragInfo.SourceCollection).Remove(pupil);
        }

        public ICollectionView Schools { get; private set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions