Export (0) Print
Expand All

DataObject Class

Provides a basic implementation of the IDataObject interface, which defines a format-independent mechanism for transferring data.

System.Object
  System.Windows.DataObject

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

public sealed class DataObject : IDataObject

The DataObject type exposes the following members.

  NameDescription
Public methodDataObject()Initializes a new instance of the DataObject class.
Public methodDataObject(Object)Initializes a new instance of the DataObject class, with specified initial data.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetData(String)Retrieves a data object in a specified format; the data format is specified by a string.
Public methodGetData(Type)Retrieves a data object in a specified format; the data format is specified by a Type object. Always throws an exception.
Public methodGetData(String, Boolean)Retrieves a data object in a specified format, optionally converting the data to the specified format. Always throws an exception.
Public methodGetDataPresent(String)Checks to see whether the data is available in, or can be converted to, a specified format; the data format is specified by a string.
Public methodGetDataPresent(Type)Checks whether the data is available in, or can be converted to, a specified format. The data format is specified by a Type object. Always throws an exception.
Public methodGetDataPresent(String, Boolean)Checks whether the data is available in, or can be converted to, a specified format. A Boolean flag indicates whether to check if the data can be converted to the specified format, if it is not available in that format. Always throws an exception.
Public methodGetFormats()Returns a list of all formats that the data in this data object is stored in, or can be converted to.
Public methodGetFormats(Boolean)Returns a list of all formats that the data in this data object is stored in. A Boolean flag indicates whether to also include formats that the data can be automatically converted to. Always throws an exception.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetData(Object)Stores the specified data in this data object and automatically converts the data format from the source object type. Always throws an exception.
Public methodSetData(String, Object)Stores the specified data in this data object, along with one or more specified data formats. The data format is specified by a string. Always throws an exception.
Public methodSetData(Type, Object)Stores the specified data in this data object, together with one or more specified data formats. The data format is specified by a Type class. Always throws an exception.
Public methodSetData(String, Object, Boolean)Stores the specified data in this data object, together with one or more specified data formats. This overload includes a Boolean flag to indicate whether the data may be converted to another format on retrieval. Always throws an exception.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The use of DataObject in Silverlight 5 is limited to a particular scenario: enabling a Silverlight 5 UIElement to be a drop target for file list information. The UIElement.Drop event carries event data that provides the data object as an instance of IDataObject, which DataObject implements. To support this scenario, GetData(String) is the primary API of interest, and you call this method against the object that is obtained from DragEventArgs.Data.

In Silverlight 5, DataObject supports only one data format, FileDrop. That format is specified as a string parameter in the DataObject API. (You should use the constant DataFormats.FileDrop to provide that string.) DataObject does not implement any IDataObject APIs that use Type in a signature or that support automatic formatting. DataObject also does not implement any SetData signature; access to the data object for the drop target scenario is read-only.

Access to the DataObject API is only permitted when the mode is Drop (the data was obtained as the event data of a Drop event). In all other cases, including cases where the event data came from the other drag-related events DragEnter, DragOver or DragLeave, calling any DataObject API throws a SecurityException.

The following example demonstrates a Drop event handler that obtains FileInfo results from the event data and its DataObject content.

This particular example is a handler on a StackPanel. The intention is that users drag a file list of images from the local computer file system onto this panel. The panel then displays each such image file by creating a new image from the stream you get from the FileInfo objects and adding the images to the panel so that they display in the Silverlight content area. (Full logic for assuring that the files are valid images that can render in Silverlight is deliberately not shown in this example. For more information about how to verify image display through events and other techniques, see Imaging or Image.

private void DisplayPanel_Drop(object sender, DragEventArgs e)
{
    StackPanel sp = sender as StackPanel;
    String[] dropFormatArray = e.Data.GetFormats();
    if (dropFormatArray.Contains("FileDrop"))
    {
        object dropObjectArray = e.Data.GetData("FileDrop");
        FileInfo[] fileObj = dropObjectArray as FileInfo[];
        foreach (FileInfo fi in fileObj)
        {
            FileStream dropFile = fi.OpenRead();
            try
            {
                Image image = new Image();
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(dropFile);
                image.Source = bitmapImage;
                sp.Children.Add(image);
            }
            catch (Exception ex)
            {
                 //stream errors
                 //might also want to handle ImageFailed on added images
                 //so can prompt user about format errors etc.
             }
        }
    }
}

Silverlight

Supported in: 5, 4

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft