DataObject.GetDataPresent Method (String, Boolean)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

public bool GetDataPresent(
	string format,
	bool autoConvert
)

Parameters

format
Type: System.String

A string that specifies the data format to check. For a set of predefined data formats, see the DataFormats class.

autoConvert
Type: System.Boolean

false to check only for the specified format; true to also check whether data stored in this data object can be converted to the specified format.

Return Value

Type: System.Boolean

true if the data is in, or can be converted to, the specified format; otherwise, false.

Exception Condition
ArgumentNullException

format is null.

Call GetDataPresent to determine whether a format is available in this data object before calling GetData. Call GetFormats to obtain a list of all the formats that are available in this data object.

The following example uses this method to query for data by descriptor string, and specifies how to treat auto-convertible data formats.

DataObject dataObject = new DataObject("Some string data to store...");

// Query for the presence of Text data in the data object, by data format descriptor string,
// and specifying whether auto-convertible data formats are acceptable.  

// In this case, Text data is present natively, so GetDataPresent returns "true".
string textData = null;
if (dataObject.GetDataPresent(DataFormats.Text, false /* Auto-convert? */))
{
    textData = dataObject.GetData(DataFormats.Text) as string;
}

// In this case, the Text data in the data object can be autoconverted to 
// Unicode text, but it is not available natively, so GetDataPresent returns "false".
byte[] unicodeData = null;
if (dataObject.GetDataPresent(DataFormats.UnicodeText, false /* Auto-convert? */))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}

// In this case, the Text data in the data object can be autoconverted to 
// Unicode text, so GetDataPresent returns "true".
if (dataObject.GetDataPresent(DataFormats.UnicodeText, true /* Auto-convert? */))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}

.NET Framework
Available since 3.0
Silverlight
Available since 4.0
Return to top
Show: