次の方法で共有


データとデータ オブジェクト

ドラッグ アンド ドロップ操作の一部として転送されるデータは、データ オブジェクトに格納されます。 概念的には、データ オブジェクトは次のペアの 1 つ以上で構成されます。

  • 実際のデータを含む Object

  • 対応するデータ形式識別子。

データ自体は、基本 Objectとして表すことができるもので構成できます。 対応するデータ形式は、データの形式に関するヒントを提供する文字列または Type です。 データ オブジェクトは、複数のデータ/データ形式のペアのホストをサポートします。これにより、1 つのデータ オブジェクトで複数の形式のデータを提供できます。

データ オブジェクト

すべてのデータ オブジェクトは、 IDataObject インターフェイスを実装する必要があります。このインターフェイスには、データ転送を有効にして容易にする次の標準的なメソッドセットが用意されています。

メソッド 概要
GetData 指定したデータ形式のデータ オブジェクトを取得します。
GetDataPresent 指定した形式でデータを使用できるかどうか、またはデータを変換できるかどうかを確認します。
GetFormats このデータ オブジェクト内のデータが格納されている、または変換できる形式の一覧を返します。
SetData 指定したデータをこのデータ オブジェクトに格納します。

WPF は、IDataObject クラスでのDataObjectの基本的な実装を提供します。 ストック DataObject クラスは、多くの一般的なデータ転送シナリオで十分です。

ビットマップ、CSV、ファイル、HTML、RTF、文字列、テキスト、オーディオなど、いくつかの定義済みの形式があります。 WPF で提供される定義済みのデータ形式については、 DataFormats クラスのリファレンス トピックを参照してください。

データ オブジェクトには、通常、データを抽出する際に、1 つの形式で格納されているデータを別の形式に自動的に変換するための機能が含まれます。この機能は自動変換と呼ばれます。 データ オブジェクトで使用可能なデータ形式のクエリを実行する場合、 GetFormats(Boolean) メソッドまたは GetDataPresent(String, Boolean) メソッドを呼び出し、 autoConvert パラメーターを falseとして指定することで、自動変換可能なデータ形式をネイティブ データ形式からフィルター処理できます。 SetData(String, Object, Boolean)メソッドを使用してデータ オブジェクトにデータを追加する場合、autoConvert パラメーターを false に設定することで、データの自動変換を禁止できます。

データ オブジェクトの操作

このセクションでは、データ オブジェクトを作成および操作するための一般的な手法について説明します。

新しいデータ オブジェクトの作成

DataObject クラスには、新しいDataObject インスタンスに単一のデータ/データ形式のペアを設定しやすくするオーバーロードされたコンストラクターがいくつか用意されています。

次のコード例では、新しいデータ オブジェクトを作成し、オーバーロードされたコンストラクターの 1 つ DataObject(DataObject(String, Object)) を使用して、文字列と指定したデータ形式でデータ オブジェクトを初期化します。 この場合、データ形式は文字列で指定されます。 DataFormats クラスは、定義済みの型文字列のセットを提供します。 既定では、格納されているデータの自動変換が許可されます。

string stringData = "Some string data to store...";
string dataFormat = DataFormats.UnicodeText;
DataObject dataObject = new DataObject(dataFormat, stringData);
Dim stringData As String = "Some string data to store..."
Dim dataFormat As String = DataFormats.UnicodeText
Dim dataObject As New DataObject(dataFormat, stringData)

データ オブジェクトを作成するコードの例については、「データ オブジェクトの 作成」を参照してください。

複数の形式でデータを格納する

1 つのデータ オブジェクトは、複数の形式でデータを格納できます。 1 つのデータ オブジェクト内で複数のデータ形式を戦略的に使用すると、1 つのデータ形式のみを表すことができる場合よりも、さまざまなドロップ ターゲットによってデータ オブジェクトが消費される可能性があります。 一般に、ドラッグ ソースは、潜在的なドロップ ターゲットによって消費されるデータ形式に依存しない必要があることに注意してください。

次の例では、SetData(String, Object) メソッドを使用して、複数の形式のデータ オブジェクトにデータを追加する方法を示します。

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

// Encode the source string into Unicode byte arrays.
byte[] unicodeText = Encoding.Unicode.GetBytes(sourceData); // UTF-16
byte[] utf8Text = Encoding.UTF8.GetBytes(sourceData);
byte[] utf32Text = Encoding.UTF32.GetBytes(sourceData);

// The DataFormats class does not provide data format fields for denoting
// UTF-32 and UTF-8, which are seldom used in practice; the following strings
// will be used to identify these "custom" data formats.
string utf32DataFormat = "UTF-32";
string utf8DataFormat  = "UTF-8";

// Store the text in the data object, letting the data object choose
// the data format (which will be DataFormats.Text in this case).
dataObject.SetData(sourceData);
// Store the Unicode text in the data object.  Text data can be automatically
// converted to Unicode (UTF-16 / UCS-2) format on extraction from the data object;
// Therefore, explicitly converting the source text to Unicode is generally unnecessary, and
// is done here as an exercise only.
dataObject.SetData(DataFormats.UnicodeText, unicodeText);
// Store the UTF-8 text in the data object...
dataObject.SetData(utf8DataFormat, utf8Text);
// Store the UTF-32 text in the data object...
dataObject.SetData(utf32DataFormat, utf32Text);
Dim dataObject As New DataObject()
Dim sourceData As String = "Some string data to store..."

' Encode the source string into Unicode byte arrays.
Dim unicodeText() As Byte = Encoding.Unicode.GetBytes(sourceData) ' UTF-16
Dim utf8Text() As Byte = Encoding.UTF8.GetBytes(sourceData)
Dim utf32Text() As Byte = Encoding.UTF32.GetBytes(sourceData)

' The DataFormats class does not provide data format fields for denoting
' UTF-32 and UTF-8, which are seldom used in practice; the following strings 
' will be used to identify these "custom" data formats.
Dim utf32DataFormat As String = "UTF-32"
Dim utf8DataFormat As String = "UTF-8"

' Store the text in the data object, letting the data object choose
' the data format (which will be DataFormats.Text in this case).
dataObject.SetData(sourceData)
' Store the Unicode text in the data object.  Text data can be automatically
' converted to Unicode (UTF-16 / UCS-2) format on extraction from the data object; 
' Therefore, explicitly converting the source text to Unicode is generally unnecessary, and
' is done here as an exercise only.
dataObject.SetData(DataFormats.UnicodeText, unicodeText)
' Store the UTF-8 text in the data object...
dataObject.SetData(utf8DataFormat, utf8Text)
' Store the UTF-32 text in the data object...
dataObject.SetData(utf32DataFormat, utf32Text)

使用可能な形式に対するデータ オブジェクトのクエリ

1 つのデータ オブジェクトに任意の数のデータ形式を含めることができるため、データ オブジェクトには、使用可能なデータ形式の一覧を取得するための機能が含まれます。

次のコード例では、 GetFormats オーバーロードを使用して、データ オブジェクトで使用可能なすべてのデータ形式を示す文字列の配列を取得します (ネイティブと自動変換の両方)。

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

// Get an array of strings, each string denoting a data format
// that is available in the data object.  This overload of GetDataFormats
// returns all available data formats, native and auto-convertible.
string[] dataFormats = dataObject.GetFormats();

// Get the number of data formats present in the data object, including both
// auto-convertible and native data formats.
int numberOfDataFormats = dataFormats.Length;

// To enumerate the resulting array of data formats, and take some action when
// a particular data format is found, use a code structure similar to the following.
foreach (string dataFormat in dataFormats)
{
    if (dataFormat == DataFormats.Text)
    {
        // Take some action if/when data in the Text data format is found.
        break;
    }
    else if(dataFormat == DataFormats.StringFormat)
    {
        // Take some action if/when data in the string data format is found.
        break;
    }
}
Dim dataObject As New DataObject("Some string data to store...")

' Get an array of strings, each string denoting a data format
' that is available in the data object.  This overload of GetDataFormats
' returns all available data formats, native and auto-convertible.
Dim dataFormats() As String = dataObject.GetFormats()

' Get the number of data formats present in the data object, including both
' auto-convertible and native data formats.
Dim numberOfDataFormats As Integer = dataFormats.Length

' To enumerate the resulting array of data formats, and take some action when
' a particular data format is found, use a code structure similar to the following.
For Each dataFormat As String In dataFormats
    If dataFormat = System.Windows.DataFormats.Text Then
        ' Take some action if/when data in the Text data format is found.
        Exit For
    ElseIf dataFormat = System.Windows.DataFormats.StringFormat Then
        ' Take some action if/when data in the string data format is found.
        Exit For
    End If
Next dataFormat

データ オブジェクトに対してクエリを実行して使用可能なデータ形式を取得するコードの例については、「データ オブジェクトのデータ形式の一覧表示」を参照してください。 特定のデータ形式が存在するかどうかをデータ オブジェクトに照会する例については、「データ オブジェクトに データ形式が存在するかどうかを判断する」を参照してください。

データ オブジェクトからのデータの取得

特定の形式のデータ オブジェクトからデータを取得するには、 GetData メソッドの 1 つを呼び出し、目的のデータ形式を指定するだけです。 GetDataPresentメソッドの 1 つを使用して、特定のデータ形式の有無を確認できます。 GetDataObjectでデータを返します。データ形式によっては、このオブジェクトを型固有のコンテナーにキャストできます。

次のコード例では、 GetDataPresent(String) オーバーロードを使用して、指定したデータ形式が使用可能かどうかを (ネイティブまたは自動変換によって) 確認します。 指定した形式が使用可能な場合、この例では、 GetData(String) メソッドを使用してデータを取得します。

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

string desiredFormat = DataFormats.UnicodeText;
byte[] data = null;

// Use the GetDataPresent method to check for the presence of a desired data format.
// This particular overload of GetDataPresent looks for both native and auto-convertible
// data formats.
if (dataObject.GetDataPresent(desiredFormat))
{
    // If the desired data format is present, use one of the GetData methods to retrieve the
    // data from the data object.
    data = dataObject.GetData(desiredFormat) as byte[];
}
Dim dataObject As New DataObject("Some string data to store...")

Dim desiredFormat As String = DataFormats.UnicodeText
Dim data() As Byte = Nothing

' Use the GetDataPresent method to check for the presence of a desired data format.
' This particular overload of GetDataPresent looks for both native and auto-convertible 
' data formats.
If dataObject.GetDataPresent(desiredFormat) Then
    ' If the desired data format is present, use one of the GetData methods to retrieve the
    ' data from the data object.
    data = TryCast(dataObject.GetData(desiredFormat), Byte())
End If

データ オブジェクトからデータを取得するコードのその他の例については、「特定のデータ 形式のデータを取得する」を参照してください。

データ オブジェクトからのデータの削除

データ オブジェクトからデータを直接削除することはできません。 データ オブジェクトからデータを効果的に削除するには、次の手順に従います。

  1. 保持するデータのみを含む新しいデータ オブジェクトを作成します。

  2. 古いデータ オブジェクトから新しいデータ オブジェクトに目的のデータを "コピー" します。 データをコピーするには、 GetData メソッドのいずれかを使用して生データを含む Object を取得し、 SetData メソッドのいずれかを使用して新しいデータ オブジェクトにデータを追加します。

  3. 古いデータ オブジェクトを新しいデータ オブジェクトに置き換えます。

SetData メソッドは、データ オブジェクトにのみデータを追加します。データとデータ形式が以前の呼び出しとまったく同じである場合でも、データは置き換えされません。 同じデータ形式とデータ形式に対して SetData を 2 回呼び出すと、データ/データ形式がデータ オブジェクトに 2 回表示されます。