Partager via


DataObject Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

System.Object
  System.Windows.DataObject

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

Syntax

'Declaration
Public NotInheritable Class DataObject _
    Implements IDataObject
public sealed class DataObject : IDataObject

The DataObject type exposes the following members.

Constructors

  Name Description
Public method DataObject() Initializes a new instance of the DataObject class.
Public method DataObject(Object) Initializes a new instance of the DataObject class, with specified initial data.

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetData(String) Retrieves a data object in a specified format; the data format is specified by a string.
Public method GetData(Type) Retrieves a data object in a specified format; the data format is specified by a Type object. Always throws an exception.
Public method GetData(String, Boolean) Retrieves a data object in a specified format, optionally converting the data to the specified format. Always throws an exception.
Public method GetDataPresent(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 method GetDataPresent(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 method GetDataPresent(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 method GetFormats() Returns a list of all formats that the data in this data object is stored in, or can be converted to.
Public method GetFormats(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 method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetData(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 method SetData(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 method SetData(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 method SetData(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 method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

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.

Examples

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.
             }
        }
    }
}

Version Information

Silverlight

Supported in: 5, 4

Platforms

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

Thread Safety

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

See Also

Reference

Other Resources