#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;
        }
    }

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: