ITypedList Antarmuka
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menyediakan fungsionalitas untuk menemukan skema untuk daftar yang dapat diikat, di mana properti yang tersedia untuk pengikatan berbeda dari properti publik objek yang akan diikat.
public interface class ITypedList
public interface ITypedList
type ITypedList = interface
Public Interface ITypedList
- Turunan
Contoh
Contoh kode berikut menunjukkan cara mengimplementasikan ITypedList antarmuka. Jenis generik bernama SortableBindingList
berasal dari BindingList<T> kelas dan mengimplementasikan ITypedList antarmuka. Untuk daftar kode lengkap, lihat Cara: Menerapkan Antarmuka ITypedList.
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
Keterangan
Gunakan antarmuka ini jika, misalnya, Anda menggunakan DataView objek yang mewakili customer
tabel, Anda ingin mengikat properti pada objek yang DataView diwakilicustomer
, bukan properti .DataView
Antarmuka ini tidak diperlukan untuk dukungan waktu desain dari daftar yang dapat diikat.
Pengikatan ke data dapat terjadi baik pada durasi atau dalam perancang, tetapi ada aturan untuk keduanya. Pada durasi, Anda dapat mengikat data dalam salah satu hal berikut ini:
Pelaksana dari IList, asalkan pelaksana memiliki properti yang sangat di ketik Item[] (artinya, Type apa saja kecuali Object). Anda dapat menyelesaikan ini dengan membuat implementasi Item[] default privat. Jika Anda ingin membuat IList yang mengikuti aturan koleksi yang ditik dengan kuat, Anda harus berasal dari CollectionBase.
Pelaksana .ITypedList
Dalam perancang, Anda dapat menginisialisasi pengikatan ke Component objek dengan mengikuti aturan yang sama.
Untuk informasi selengkapnya tentang pengikatan ke sumber data, lihat System.Windows.Forms.Binding kelas .
Metode
GetItemProperties(PropertyDescriptor[]) |
Mengembalikan PropertyDescriptorCollection yang mewakili properti pada setiap item yang digunakan untuk mengikat data. |
GetListName(PropertyDescriptor[]) |
Mengembalikan nama daftar. |