#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"
                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"/>

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()
            this.DataContext = this;

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

        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.



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

    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;

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #705 – Dragging a Custom Object Using Serialization as Format

  1. Pingback: Dew Drop – December 5, 2012 (#1,456) | Alvin Ashcraft's Morning Dew

  2. Pingback: #728 – Using the Clipboard to Transfer Other Types of Data « 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: