컬렉션 편집기 예제
업데이트: 2007년 11월
이 예제에서는 사용자 지정 컬렉션 편집기를 구현하는 ContactCollectionEditor 컨트롤을 만드는 방법을 보여 줍니다. 또한 사용자 지정 컬렉션 편집기를 사용하는 경우 페이지 개발자가 컨트롤의 컬렉션 속성에 추가할 수 있는 개체의 정확한 형식을 지정하는 방법을 보여 줍니다. EditorAttribute를 컨트롤의 컬렉션 속성에 적용하여 컬렉션 편집기를 컬렉션 속성 또는 이 속성의 형식과 연결합니다.
컬렉션 속성에 대한 강력한 형식의 IList 구현을 사용하고 이 컬렉션에 있는 모든 개체의 형식이 같은 경우에는 사용자 지정 컬렉션 편집기가 필요하지 않습니다. 이런 경우에는 기본 제공된 CollectionEditor를 속성 편집기로 사용할 수 있습니다. CollectionEditor는 IList 구현의 Items 속성 형식에서 개체 형식을 유추할 수 있기 때문입니다. 가능하면 형식화된 컬렉션을 사용해야 합니다. 그러나 ArrayList 같은 컬렉션을 컨트롤의 컬렉션 속성에 대한 형식으로 사용할 경우에는 컬렉션 항목의 개체 형식을 지정하기 위해 사용자 지정 컬렉션 편집기가 필요합니다.
이 예제에서 설명하는 ContactCollectionEditor는 웹 컨트롤 컬렉션 속성 예제에 설명된 QuickContacts 컨트롤의 Contacts 속성에서 사용됩니다. 이 컨트롤을 사용하면 컬렉션 편집기 UI(사용자 인터페이스)를 통해 Contact 형식의 개체를 Contacts 속성에 추가할 수 있습니다. ContactCollectionEditor 클래스는CollectionEditor에서 파생되고 CreateCollectionItemType 메서드를 재정의하여 Contact 형식을 반환합니다.
컨트롤의 컬렉션 속성에 형식이 다른 개체가 포함된 경우에는 이 예제와 비슷한 컬렉션 편집기를 구현하되 CreateCollectionItemType 메서드 대신 CreateNewItemTypes 메서드를 구현하여 정확한 항목 형식을 반환합니다.
ContactCollectionEditor에 대한 코드 목록
' ContactCollectionEditor.vb
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Reflection
Namespace Samples.AspNet.VB.Controls
Public Class ContactCollectionEditor
Inherits CollectionEditor
Public Sub New(ByVal newType As Type)
MyBase.new(newType)
End Sub
Protected Overrides Function CanSelectMultipleInstances() _
As Boolean
Return False
End Function
Protected Overrides Function CreateCollectionItemType() As Type
Return GetType(Contact)
End Function
End Class
End Namespace
// ContactCollectionEditor.cs
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
namespace Samples.AspNet.CS.Controls
{
public class ContactCollectionEditor : CollectionEditor
{
public ContactCollectionEditor(Type type)
: base(type)
{
}
protected override bool CanSelectMultipleInstances()
{
return false;
}
protected override Type CreateCollectionItemType()
{
return typeof(Contact);
}
}
}
예제 빌드 및 사용
웹 컨트롤 컬렉션 속성 예제에 나열된 QuickContacts 컨트롤과 Contacts 클래스를 사용하여 ContactCollectionEditor 편집기를 컴파일합니다. 컴파일에 사용할 System.Design 어셈블리에 대한 참조를 추가해야 합니다.
사용자 지정 컨트롤 예제의 컴파일 및 사용에 대한 자세한 내용은 사용자 지정 서버 컨트롤 예제 빌드를 참조하십시오.