BindingSource クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文Public Class BindingSource Inherits Component Implements IBindingListView, IBindingList, IList, ICollection, _ IEnumerable, ITypedList, ICancelAddNew, ISupportInitializeNotification, ISupportInitialize, _ ICurrencyManagerProvider
public class BindingSource : Component, IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ICancelAddNew, ISupportInitializeNotification, ISupportInitialize, ICurrencyManagerProvider
public ref class BindingSource : public Component, IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ICancelAddNew, ISupportInitializeNotification, ISupportInitialize, ICurrencyManagerProvider
解説BindingSource コンポーネントは、2 つの目的を果たします。1 つは、間接層、通貨管理、変更通知、およびその他のサービスを提供して、フォーム上のコントロールのデータへのバインディングを簡素化することです。これは、BindingSource コンポーネントをデータ ソースに結びつけ、フォーム上のコントロールを BindingSource コンポーネントにバインディングすることによって達成されます。以降のデータとの対話 (移動、並べ替え、フィルタ処理、更新など) は、BindingSource コンポーネントを呼び出すことによって実行します。
BindingSource コンポーネントが果たすもう 1 つの目的は、厳密に型指定されたデータ ソースとして機能することです。基底のデータ ソースの型は、通常、次の機構のうちいずれかを通じて固定されます。
どちらの機構を使用しても、厳密に型指定されたリストが作成されます。BindingSource は、DataSource プロパティと DataMember プロパティの設定に従い、単純データ バインディングと複合データ バインディングの両方をサポートします。
注意 |
|---|
| 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照- BindingSource クラスのページへのリンク
.gif)