How do I create a DataObject?

Platform: WPF|Category: Drag and Drop

A DataObject can be created in three different overloads of the DataObject constructor.

  • DataObject (object) – In this case, appropriate DataFormat is assigned to the DataObject based on the type of data stored in the object. By default, auto-conversion option is set to ’true’.
[C#]

string mystring = 'Drag and Drop String Object';
DataObject dataobject = new DataObject(mystring);
  • DataObject (DataFormat, object) – In this case, DataFormat is mentioned when the DataObject is created.
[C#]

string mystring = 'Drag and Drop String Object';
DataObject dataobject = new DataObject(DataFormats.UnicodeText,mystring);
  • DataObject (DataFormat, Object, auto-conversion) – In this case, DataFormat, Object and Auto-Conversion Boolean value are mentioned when the DataObject is created.
[C#]

string mystring = 'Drag and Drop String Object';
DataObject dataobject = new DataObject(DataFormats.UnicodeText,mystring,false);