데이터 및 데이터 개체
업데이트: 2011년 4월
끌어서 놓기 작업의 일부로 전송되는 데이터는 데이터 개체에 저장됩니다. 개념적으로 보면 데이터 개체는 다음 쌍 중 하나 이상으로 구성됩니다.
실제 데이터가 포함된 Object
해당 데이터 형식 식별자
데이터 자체는 기본 Object로 나타낼 수 있는 모든 것으로 구성될 수 있습니다. 해당 데이터 형식은 데이터의 형식에 대한 힌트를 제공하는 문자열 또는 Type입니다. 데이터 개체에는 여러 데이터/데이터 형식 쌍에 대한 호스팅이 지원됩니다. 따라서 단일 데이터 개체가 여러 형식의 데이터를 제공할 수 있습니다.
데이터 개체
모든 데이터 개체는 데이터 전송을 사용 가능 및 용이하게 하는 다음 표준 메서드 집합을 제공하는 IDataObject 인터페이스를 구현해야 합니다.
메서드 |
요약 |
---|---|
지정된 데이터 형식의 데이터 개체를 검색합니다. |
|
지정된 형식의 데이터가 있거나 지정된 형식으로 변환될 수 있는지 확인합니다. |
|
이 데이터 개체의 데이터가 저장되거나 변환될 수 있는 형식 목록을 반환합니다. |
|
이 데이터 개체에 지정된 데이터를 저장합니다. |
WPF는 DataObject 클래스에서 IDataObject의 기본 구현을 제공합니다. 스톡 DataObject 클래스는 대부분의 일반 데이터 전송 시나리오에 충분합니다.
비트맵, CSV, 파일, HTML, RTF, 문자열, 텍스트, 오디오 등의 미리 정의된 여러 형식이 있습니다. WPF와 함께 제공되는 미리 정의된 데이터 형식에 대한 자세한 내용은 DataFormats 클래스 참조 항목을 참조하십시오.
일반적으로 데이터 개체에는 데이터를 추출하는 동안 한 형식으로 저장된 데이터를 다른 형식으로 자동 변환하는 기능이 포함되어 있습니다. 이 기능을 자동 변환이라고 합니다. 데이터 개체에서 사용할 수 있는 데이터 형식을 쿼리할 경우 GetFormats 또는 GetDataPresent 메서드를 호출하고 autoConvert 매개 변수를 false로 지정하여 자동 변환할 수 있는 데이터 형식을 네이티브 데이터 형식으로 필터링할 수 있습니다. SetData 메서드를 사용하여 데이터를 데이터 개체에 추가할 경우 autoConvert 매개 변수를 false로 설정하여 데이터의 자동 변환을 방지할 수 있습니다.
데이터 개체 작업
이 단원에서는 데이터 개체 만들기 및 작업을 위한 일반적인 기술에 대해 설명합니다.
새 데이터 개체 만들기
DataObject 클래스는 새 DataObject 인스턴스를 단일 데이터/데이터 형식 쌍으로 쉽게 채울 수 있게 하는 여러 오버로드된 생성자를 제공합니다.
다음 예제 코드에서는 새 데이터 개체를 만들고 오버로드된 생성자 DataObject(DataObject) 중 하나를 사용하여 문자열 및 지정된 데이터 형식으로 데이터 개체를 초기화합니다. 이 경우 데이터 형식은 문자열로 지정되며 DataFormats 클래스는 미리 정의된 형식 문자열 집합을 제공합니다. 저장된 데이터는 기본적으로 자동 변환됩니다.
Dim stringData As String = "Some string data to store..."
Dim dataFormat As String = DataFormats.UnicodeText
Dim dataObject As New DataObject(dataFormat, stringData)
string stringData = "Some string data to store...";
string dataFormat = DataFormats.UnicodeText;
DataObject dataObject = new DataObject(dataFormat, stringData);
데이터 개체를 만드는 추가 코드 예제는 방법: 데이터 개체 만들기를 참조하십시오.
데이터를 여러 형식으로 저장
단일 데이터 개체에서 데이터를 여러 형식으로 저장할 수 있습니다. 단일 데이터 개체에서 여러 데이터 형식을 전략적으로 사용하면 단일 데이터 형식만 표시할 수 있는 경우보다 다양한 놓기 대상에서 데이터 개체를 사용할 수 있습니다. 일반적으로 끌기 소스는 잠재적 놓기 대상에서 사용할 수 있는 데이터 형식과 무관해야 합니다.
다음 예제에서는 SetData 메서드를 사용하여 데이터를 여러 형식으로 데이터 개체에 추가하는 방법을 보여 줍니다.
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)
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);
데이터 개체에서 사용할 수 있는 형식 쿼리
단일 데이터 개체가 데이터 형식을 개수에 상관없이 포함할 수 있으므로 데이터 개체에는 사용할 수 있는 데이터 형식 목록을 검색하는 기능이 포함되어 있습니다.
다음 예제 코드에서는 GetFormats 오버로드를 사용하여 데이터 개체에서 기본적으로 및 자동 변환을 통해 사용할 수 있는 모든 데이터 형식을 나타내는 문자열 배열을 가져옵니다.
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
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;
}
}
데이터 개체에서 사용할 수 있는 데이터 형식을 쿼리하는 추가 코드 예제는 방법: 데이터 개체의 데이터 형식 나열을 참조하십시오. 데이터 개체에서 특정 데이터 형식의 존재를 쿼리하는 예제는 방법: 데이터 개체에 데이터 형식이 있는지 확인을 참조하십시오.
데이터 개체에서 데이터 검색
데이터 개체에서 특정 형식의 데이터를 검색하는 작업에서는 단순히 GetData 메서드 중 하나를 호출하고 원하는 데이터 형식을 지정합니다. GetDataPresent 메서드 중 하나를 사용하여 특정 데이터 형식의 존재를 확인할 수 있습니다. GetData는 Object의 데이터를 반환합니다. 데이터 형식에 따라 이 개체는 형식별 컨테이너에 캐스팅될 수 있습니다.
다음 예제 코드에서는 GetDataPresent 오버로드를 사용하여 지정된 데이터 형식을 사용할 수 있는지(기본적으로 또는 자동 변환에 의해) 여부를 확인합니다. 지정된 형식을 사용할 수 있는 경우 이 예제는 GetData 메서드를 사용하여 데이터를 검색합니다.
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
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[];
}
데이터 개체에서 데이터를 검색하는 추가 코드 예제는 방법: 특정 데이터 형식의 데이터 검색을 참조하십시오.
데이터 개체에서 데이터 제거
데이터를 데이터 개체에서 직접 제거할 수 없습니다. 데이터 개체에서 데이터를 효율적으로 제거하려면 다음 단계를 수행합니다.
보유할 데이터만 포함할 새 데이터 개체를 만듭니다.
이전 데이터 개체에서 새 데이터 개체로 원하는 데이터를 "복사"합니다. 데이터를 복사하려면 GetData 메서드 중 하나를 사용하여 원시 데이터를 포함하는 Object를 검색한 다음 SetData 메서드 중 하나를 사용하여 데이터를 새 데이터 개체에 추가합니다.
이전 데이터 개체를 새 데이터 개체로 바꿉니다.
참고 |
---|
SetData 메서드는 데이터를 단지 데이터 개체에 추가할 뿐이여 데이터 및 데이터 형식이 이전 호출과 같은 경우에도 데이터를 바꾸지 않습니다.동일한 데이터 및 데이터 형식에 대해 SetData를 두 번 호출하면 데이터/데이터 형식이 데이터 개체에 두 번 존재하게 됩니다. |
변경 기록
날짜 |
변경 내용 |
이유 |
---|---|---|
2011년 4월 |
항목이 추가되었습니다. |
고객 의견 |