ITypedList 介面
定義
提供發現可繫結清單的結構描述 (Schema) 的功能,其中可用於繫結的屬性與要繫結物件的公用 (Public) 屬性是不同的。Provides functionality to discover the schema for a bindable list, where the properties available for binding differ from the public properties of the object to bind to.
public interface class ITypedList
public interface ITypedList
type ITypedList = interface
Public Interface ITypedList
- 衍生
範例
下列程式碼範例示範如何執行 ITypedList 介面。The following code example demonstrates how to implement the ITypedList interface. 名為的泛型型別 SortableBindingList
衍生自 BindingList<T> 類別,並實作為 ITypedList 介面。A generic type named SortableBindingList
derives from the BindingList<T> class and implements the ITypedList interface. 如需完整的程式代碼清單,請參閱 how to:執行 ITypedList 介面。For a full code listing, see How to: Implement the ITypedList Interface.
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
namespace ITypedListCS
{
[Serializable()]
public class SortableBindingList<T> : BindingList<T>, ITypedList
{
[NonSerialized()]
private PropertyDescriptorCollection properties;
public SortableBindingList() : base()
{
// Get the 'shape' of the list.
// Only get the public properties marked with Browsable = true.
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(
typeof(T),
new Attribute[] { new BrowsableAttribute(true) });
// Sort the properties.
properties = pdc.Sort();
}
#region ITypedList Implementation
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
PropertyDescriptorCollection pdc;
if (listAccessors!=null && listAccessors.Length>0)
{
// Return child list shape.
pdc = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);
}
else
{
// Return properties in sort order.
pdc = properties;
}
return pdc;
}
// This method is only used in the design-time framework
// and by the obsolete DataGrid control.
public string GetListName(PropertyDescriptor[] listAccessors)
{
return typeof(T).Name;
}
#endregion
}
}
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Windows.Forms
<Serializable()> _
Public Class SortableBindingList(Of Tkey)
Inherits BindingList(Of Tkey)
Implements ITypedList
<NonSerialized()> _
Private properties As PropertyDescriptorCollection
Public Sub New()
MyBase.New()
' Get the 'shape' of the list.
' Only get the public properties marked with Browsable = true.
Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(Tkey), New Attribute() {New BrowsableAttribute(True)})
' Sort the properties.
properties = pdc.Sort()
End Sub
#Region "ITypedList Implementation"
Public Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties
Dim pdc As PropertyDescriptorCollection
If (Not (listAccessors Is Nothing)) And (listAccessors.Length > 0) Then
' Return child list shape
pdc = ListBindingHelper.GetListItemProperties(listAccessors(0).PropertyType)
Else
' Return properties in sort order
pdc = properties
End If
Return pdc
End Function
' This method is only used in the design-time framework
' and by the obsolete DataGrid control.
Public Function GetListName( _
ByVal listAccessors() As PropertyDescriptor) As String _
Implements System.ComponentModel.ITypedList.GetListName
Return GetType(Tkey).Name
End Function
#End Region
End Class
備註
如果您是使用 DataView 代表資料表的物件,而您想要系結 customer
至 customer
所代表物件的屬性 DataView ,而不 DataView 是的屬性,請使用此介面。Use this interface if, for instance, you are using a DataView object that represents a customer
table, you want to bind to the properties on the customer
object that the DataView represents, not the properties of the DataView.
可系結清單的設計階段支援不需要此介面。This interface is not required for design-time support of a bindable list.
系結至資料可能會在執行時間或設計工具中發生,但兩者都有規則。Binding to data can occur either at run time or in a designer, but there are rules for both. 在執行時間,您可以系結至下列任一項的資料:At run time, you can bind to data in any of the following:
的實施者 IList ,前提是實施者具有強型別 Item[] 屬性 (也就 Type 是,但 Object) 。Implementer of IList, provided the implementer has a strongly typed Item[] property (that is, the Type is anything but Object). 您可以藉由進行私用的預設實值來完成這項工作 Item[] 。You can accomplish this by making the default implementation of Item[] private. 如果您想要建立 IList 遵循強型別集合規則的,您應該衍生自 CollectionBase 。If you want to create an IList that follows the rules of a strongly typed collection, you should derive from CollectionBase.
的實施者 ITypedList 。Implementer of ITypedList.
在設計工具中,您可以 Component 遵循相同的規則,初始化物件的系結。In a designer, you can initialize binding to Component objects by following the same rules.
如需系結至資料來源的詳細資訊,請參閱 System.Windows.Forms.Binding 類別。For more information on binding to a data source, see the System.Windows.Forms.Binding class.
方法
GetItemProperties(PropertyDescriptor[]) |
傳回代表繫結資料所用各項目屬性的 PropertyDescriptorCollection。Returns the PropertyDescriptorCollection that represents the properties on each item used to bind data. |
GetListName(PropertyDescriptor[]) |
傳回清單的名稱。Returns the name of the list. |