#705 – Dragging a Custom Object Using Serialization as Format

When you use drag-and-drop in WPF, you specify a data format for the data to be dragged.  You can “drag” any object that you like, as long as the object is serializable.  Here’s an example, where a Dog object is dragged between instances of a WPF application.

The GUI consists of several labels that show properties of a Dog object.  The Dog can be dragged from the StackPanel or dropped onto the StackPanel.

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center"
                MouseLeftButtonDown="StackPanel_MouseLeftButtonDown"
                AllowDrop="True" Drop="StackPanel_Drop">
        <Label Content="{Binding TheDog.Name}" FontWeight="Bold"/>
        <Label Content="{Binding TheDog.Age}"/>
        <Label Content="{Binding TheDog.BarkSound}"/>
        <Button Content="Create Dog" Click="Button_Click"/>
    </StackPanel>

The code-behind creates a Dog object on the button click.  When dragging starts, the Dog is passed in to the DoDragDrop method.  In the Drop handler, the Dog data is retrieved via the IDataObject interface.

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private Dog theDog;
        public Dog TheDog
        {
            get { return theDog; }
            set
            {
                if (value != theDog)
                {
                    theDog = value;
                    RaisePropertyChanged("TheDog");
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TheDog = new Dog("Kirby", 15, "Woof");
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataObject data = new DataObject(DataFormats.Serializable, theDog);

            DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy);
        }

        private void StackPanel_Drop(object sender, DragEventArgs e)
        {
            TheDog = (Dog)e.Data.GetData(DataFormats.Serializable);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

This allows us to drag the Dog from one instance of the application to another. You could also drag objects between applications in the same way.

705-001

705-002

For completeness, here’s the code for the Dog class:

    [Serializable]
    public class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string BarkSound { get; set; }

        public Dog(string name, int age, string barkSound)
        {
            Name = name;
            Age = age;
            BarkSound = barkSound;
        }
    }