BindingSource クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
public class BindingSource : Component, IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ICancelAddNew, ISupportInitializeNotification, ISupportInitialize, ICurrencyManagerProvider
public class BindingSource extends Component implements IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ICancelAddNew, ISupportInitializeNotification, ISupportInitialize, ICurrencyManagerProvider
BindingSource コンポーネントは、2 つの目的を果たします。1 つは、間接層、通貨管理、変更通知、およびその他のサービスを提供して、フォーム上のコントロールのデータへのバインディングを簡素化することです。これは、BindingSource コンポーネントをデータ ソースに結びつけ、フォーム上のコントロールを BindingSource コンポーネントにバインディングすることによって達成されます。以降のデータとの対話 (移動、並べ替え、フィルタ処理、更新など) は、BindingSource コンポーネントを呼び出すことによって実行します。
BindingSource コンポーネントが果たすもう 1 つの目的は、厳密に型指定されたデータ ソースとして機能することです。基底のデータ ソースの型は、通常、次の機構のうちいずれかを通じて固定されます。
-
Add メソッドを使用して、項目を BindingSource コンポーネントに追加します。
-
DataSource プロパティを、リスト、単一のオブジェクト、または型に設定します。
どちらの機構を使用しても、厳密に型指定されたリストが作成されます。BindingSource は、DataSource プロパティと DataMember プロパティの設定に従い、単純データ バインディングと複合データ バインディングの両方をサポートします。
メモ |
|---|
| BindingSource は、単純データ ソースと複合データ ソースの両方を処理するので、用語上の問題が発生します。このクラスのドキュメントにおいて、リストという用語は、ホストされているデータ ソース内のデータのコレクションを指し、項目は、単一の要素を指すこととします。複合データ ソースの関連機能について説明するときは、同じ意味でテーブルおよび行という用語を使用します。 |
BindingSource には、基底のデータにアクセスするためのメンバが用意されています。現在の項目を取得する場合は Current プロパティを使用し、リスト全体を取得する場合は List プロパティを使用します。現在の項目の編集操作は、Current プロパティと、RemoveCurrent、EndEdit、CancelEdit、および Add の各メソッド、さらには AddNew メソッドを通じてサポートされています。現在性の管理は、基底のデータ ソースのすべての型について自動的に処理されますが、このクラスでは、CurrentItemChanged や DataSourceChanged などいくつかのイベントが公開されており、これによってカスタマイズが可能です。
BindingSource コンポーネントにバインドされたデータ ソースは、BindingNavigator クラスを通じてアクセスおよび管理することもできます。このクラスでは、ビデオ カセット レコーダー形式のユーザー インターフェイス (UI) を使用してリスト内の項目間を移動できます。BindingNavigator は任意のデータ ソースにバインドできますが、BindingNavigator.BindingSource プロパティを通じて BindingSource コンポーネントと統合することを前提にデザインされています。
BindingSource クラスの既定のプロパティは DataSource です。既定のイベントは CurrentChanged です。
注意 |
|---|
| BindingSource クラスの多くのメンバは、List プロパティで表される基底のリストの下で機能し、その操作を基底のリストに委託するだけです。このため、BindingSource を IList のカスタム実装とバインドした場合、これらのメンバの動作は、クラスのドキュメントに記載されている動作と厳密には異なる場合があります。たとえば、RemoveAt メソッドは、IList.RemoveAt を呼び出します。BindingSource のドキュメントにおける RemoveAt メソッドの説明は、基になる IList の RemoveAt メソッドが正しく実装されていることを前提としています。 |
BindingSource に ListBox をバインドするコード例を次に示します。BindingSource が、フォント一覧を保持する BindingList にバインドされます。
Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Drawing Imports System.Text Imports System.Windows.Forms Public Class Form1 Inherits Form <STAThread()> _ Shared Sub Main() Application.EnableVisualStyles() Application.Run(New Form1()) End Sub Public Sub New() End Sub Private textBox1 As TextBox Private WithEvents button1 As Button Private listBox1 As ListBox Private components As IContainer Private binding1 As BindingSource Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load listBox1 = New ListBox() textBox1 = New TextBox() binding1 = New BindingSource() button1 = New Button() listBox1.Location = New Point(140, 25) listBox1.Size = New Size(123, 160) textBox1.Location = New Point(23, 70) textBox1.Size = New Size(100, 20) textBox1.Text = "Wingdings" button1.Location = New Point(23, 25) button1.Size = New Size(75, 23) button1.Text = "Search" Me.ClientSize = New Size(292, 266) Me.Controls.Add(Me.button1) Me.Controls.Add(Me.textBox1) Me.Controls.Add(Me.listBox1) Dim fonts As New MyFontList() Dim i As Integer For i = 0 To FontFamily.Families.Length - 1 If FontFamily.Families(i).IsStyleAvailable(FontStyle.Regular) Then fonts.Add(New Font(FontFamily.Families(i), 11.0F, FontStyle.Regular)) End If Next i binding1.DataSource = fonts listBox1.DataSource = binding1 listBox1.DisplayMember = "Name" End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles button1.Click If binding1.SupportsSearching <> True Then MessageBox.Show("Cannot search the list.") Else Dim foundIndex As Integer = binding1.Find("Name", textBox1.Text) If foundIndex > -1 Then listBox1.SelectedIndex = foundIndex Else MessageBox.Show("Font was not found.") End If End If End Sub End Class Public Class MyFontList Inherits BindingList(Of Font) Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean Get Return True End Get End Property Protected Overrides Function FindCore(ByVal prop As PropertyDescriptor, _ ByVal key As Object) As Integer ' Ignore the prop value and search by family name. Dim i As Integer While i < Count If Items(i).FontFamily.Name.ToLower() = CStr(key).ToLower() Then Return i End If i += 1 End While Return -1 End Function End Class
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.BindingSource
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
メモ
注意