BindingContext クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
BindingManagerBase クラスから継承するすべてのオブジェクトのControl オブジェクトのコレクションを管理します。
public ref class BindingContext : System::Collections::ICollection
public class BindingContext : System.Collections.ICollection
type BindingContext = class
interface ICollection
interface IEnumerable
Public Class BindingContext
Implements ICollection
- 継承
-
BindingContext
- 実装
例
次のコード例では、4 つの Binding オブジェクトを作成して、5 つのコントロール ( DateTimePicker コントロールと 4 つの TextBox コントロール) を複数のデータ ソースにバインドします。 その後、 BindingContext を使用して、各データ ソースの BindingManagerBase を取得します。
void BindControls()
{
/* Create two Binding objects for the first two TextBox
controls. The data-bound property for both controls
is the Text property. The data source is a DataSet
(ds). The data member is a navigation path in the form:
"TableName.ColumnName". */
text1->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custName" ) );
text2->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custID" ) );
/* Bind the DateTimePicker control by adding a new Binding.
The data member of the DateTimePicker is a navigation path:
TableName.RelationName.ColumnName string. */
DateTimePicker1->DataBindings->Add( gcnew Binding( "Value",ds,"customers.CustToOrders.OrderDate" ) );
/* Add event delegates for the Parse and Format events to a
new Binding object, and add the object to the third
TextBox control's BindingsCollection. The delegates
must be added before adding the Binding to the
collection; otherwise, no formatting occurs until
the Current object of the BindingManagerBase for
the data source changes. */
Binding^ b = gcnew Binding( "Text",ds,"customers.custToOrders.OrderAmount" );
b->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyStringToDecimal );
b->Format += gcnew ConvertEventHandler( this, &Form1::DecimalToCurrencyString );
text3->DataBindings->Add( b );
// Get the BindingManagerBase for the Customers table.
bmCustomers = this->BindingContext[ ds,"Customers" ];
/* Get the BindingManagerBase for the Orders table using the
RelationName. */
bmOrders = this->BindingContext[ds, "customers.CustToOrders"];
/* Bind the fourth TextBox control's Text property to the
third control's Text property. */
text4->DataBindings->Add( "Text", text3, "Text" );
}
protected void BindControls()
{
/* Create two Binding objects for the first two TextBox
controls. The data-bound property for both controls
is the Text property. The data source is a DataSet
(ds). The data member is a navigation path in the form:
"TableName.ColumnName". */
text1.DataBindings.Add(new Binding
("Text", ds, "customers.custName"));
text2.DataBindings.Add(new Binding
("Text", ds, "customers.custID"));
/* Bind the DateTimePicker control by adding a new Binding.
The data member of the DateTimePicker is a navigation path:
TableName.RelationName.ColumnName string. */
DateTimePicker1.DataBindings.Add(new
Binding("Value", ds, "customers.CustToOrders.OrderDate"));
/* Add event delegates for the Parse and Format events to a
new Binding object, and add the object to the third
TextBox control's BindingsCollection. The delegates
must be added before adding the Binding to the
collection; otherwise, no formatting occurs until
the Current object of the BindingManagerBase for
the data source changes. */
Binding b = new Binding
("Text", ds, "customers.custToOrders.OrderAmount");
b.Parse+=new ConvertEventHandler(CurrencyStringToDecimal);
b.Format+=new ConvertEventHandler(DecimalToCurrencyString);
text3.DataBindings.Add(b);
// Get the BindingManagerBase for the Customers table.
bmCustomers = this.BindingContext [ds, "Customers"];
/* Get the BindingManagerBase for the Orders table using the
RelationName. */
bmOrders = this.BindingContext[ds, "customers.CustToOrders"];
/* Bind the fourth TextBox control's Text property to the
third control's Text property. */
text4.DataBindings.Add("Text", text3, "Text");
}
Protected Sub BindControls()
' Create two Binding objects for the first two TextBox
' controls. The data-bound property for both controls
' is the Text property. The data source is a DataSet
' (ds). The data member is the string
' "TableName.ColumnName".
text1.DataBindings.Add(New Binding _
("Text", ds, "customers.custName"))
text2.DataBindings.Add(New Binding _
("Text", ds, "customers.custID"))
' Bind the DateTimePicker control by adding a new Binding.
' The data member of the DateTimePicker is a
' TableName.RelationName.ColumnName string.
DateTimePicker1.DataBindings.Add(New Binding _
("Value", ds, "customers.CustToOrders.OrderDate"))
' Add event delegates for the Parse and Format events to a
' new Binding object, and add the object to the third
' TextBox control's BindingsCollection. The delegates
' must be added before adding the Binding to the
' collection; otherwise, no formatting occurs until
' the Current object of the BindingManagerBase for
' the data source changes.
Dim b As Binding = New Binding _
("Text", ds, "customers.custToOrders.OrderAmount")
AddHandler b.Parse, New ConvertEventHandler(AddressOf CurrencyStringToDecimal)
AddHandler b.Format, New ConvertEventHandler(AddressOf DecimalToCurrencyString)
text3.DataBindings.Add(b)
' Get the BindingManagerBase for the Customers table.
bmCustomers = Me.BindingContext(ds, "Customers")
' Get the BindingManagerBase for the Orders table using the
' RelationName.
bmOrders = Me.BindingContext(ds, "customers.CustToOrders")
' Bind the fourth TextBox control's Text property to the
' third control's Text property.
text4.DataBindings.Add("Text", text3, "Text")
End Sub
注釈
各Windows フォームには、フォームの BindingManagerBase オブジェクトを管理する少なくとも 1 つの BindingContext オブジェクトがあります。 BindingManagerBase クラスは抽象クラスであるため、Item[] プロパティの戻り値の型はCurrencyManagerまたはPropertyManagerです。 データ ソースが (オブジェクトのリストではなく) 1 つのプロパティのみを返すことができるオブジェクトである場合、 Type は PropertyManagerです。 たとえば、データ ソースとして TextBox を指定すると、 PropertyManager が返されます。 一方、データ ソースが IList または IBindingListを実装するオブジェクトである場合は、 CurrencyManager が返されます。
Windows フォーム上のデータ ソースごとに、1 つの CurrencyManager または PropertyManager があります。 Windows フォームには複数のデータ ソースが関連付けられている可能性があるため、BindingContext を使用すると、データ ソースに関連付けられている特定のCurrencyManagerを取得できます。
Note
Item[]プロパティを使用する場合、BindingContextは新しいBindingManagerBaseがまだ存在しない場合に作成します。 返されたオブジェクトは、目的のリスト (またはリスト) を管理できない可能性があるため、混乱を招く可能性があります。 無効な BindingManagerBaseが返されないようにするには、 Contains メソッドを使用して、目的の BindingManagerBase が既に存在するかどうかを判断します。
GroupBox、Panel、TabControlなどのコンテナー コントロールを使用してデータ バインド コントロールを含める場合は、そのコンテナー コントロールとそのコントロールに対するBindingContextを作成できます。 その後、フォームの各部分を独自の BindingManagerBaseで管理できます。 同じデータ ソースに対して複数のBindingManagerBase オブジェクトを作成する方法の詳細については、BindingContext コンストラクターを参照してください。
TextBox コントロールをフォームに追加し、それをデータセット内のテーブルの列にバインドすると、コントロールはそのフォームのBindingContextと通信します。
BindingContextは、そのデータ関連付けの特定のCurrencyManagerと対話します。
CurrencyManagerのPosition プロパティに対してクエリを実行すると、そのTextBox コントロールのバインドに関する現在のレコードが報告されます。 次のコード例では、TextBox コントロールは、dataSet1 データセットのCustomers テーブルのFirstName列に、そのBindingContextを使用してバインドされています。
TextBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName")
textBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName");
フォームに 2 つ目のTextBox コントロール (TextBox2) を追加し、同じデータセット内のCustomers テーブルのLastName列にバインドできます。
BindingContextは最初のバインディング (Customers.FirstNameにTextBox1) を認識しているため、両方のテキスト ボックスが同じデータセット (DataSet1) にバインドされるため、同じCurrencyManagerが使用されます。
TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")
textBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName");
TextBox2を別のデータセットにバインドすると、BindingContextは 2 つ目のCurrencyManagerを作成して管理します。
DataSourceプロパティとDisplayMemberプロパティを設定する方法の一貫性を保つ必要があります。それ以外の場合、BindingContextは同じデータセットに対して複数の通貨マネージャーを作成するため、エラーが発生します。 次のコード例は、プロパティとそれに関連付けられている BindingContext オブジェクトを設定するいくつかの方法を示しています。 コード全体で一貫性がある限り、次のいずれかのメソッドを使用してプロパティを設定できます。
ComboBox1.DataSource = DataSet1
ComboBox1.DisplayMember = "Customers.FirstName"
Me.BindingContext(dataSet1, "Customers").Position = 1
comboBox1.DataSource = DataSet1;
comboBox1.DisplayMember = "Customers.FirstName";
this.BindingContext[dataSet1, "Customers"].Position = 1;
ComboBox1.DataSource = DataSet1.Customers
ComboBox1.DisplayMember = "FirstName"
Me.BindingContext(dataSet1.Customers).Position = 1
comboBox1.DataSource = DataSet1.Customers;
comboBox1.DisplayMember = "FirstName";
this.BindingContext[dataSet1.Customers].Position = 1;
Note
ほとんどのWindows フォームアプリケーションは、BindingSourceを介してバインドされます。
BindingSource コンポーネントは、CurrencyManagerをカプセル化し、CurrencyManagerプログラミング インターフェイスを公開します。 バインドにBindingSourceを使用する場合は、BindingContextではなく、BindingSourceによって公開されているメンバーを使用して、"通貨" (つまり、Position) を操作する必要があります。
コンストラクター
| 名前 | 説明 |
|---|---|
| BindingContext() |
BindingContext クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| IsReadOnly |
コレクションが読み取り専用かどうかを示す値を取得します。 |
| Item[Object, String] |
指定したデータ ソースとデータ メンバーに関連付けられている BindingManagerBase を取得します。 |
| Item[Object] |
指定したデータ ソースに関連付けられている BindingManagerBase を取得します。 |
メソッド
イベント
| 名前 | 説明 |
|---|---|
| CollectionChanged |
処理時に常に NotImplementedException を発生させます。 |
明示的なインターフェイスの実装
| 名前 | 説明 |
|---|---|
| ICollection.CopyTo(Array, Int32) |
コレクションのインデックスから始まる指定した配列にコレクションの要素をコピーします。 |
| ICollection.Count |
BindingContextによって管理CurrencyManagerオブジェクトの合計数を取得します。 |
| ICollection.IsSynchronized |
コレクションが同期されているかどうかを示す値を取得します。 |
| ICollection.SyncRoot |
同期に使用するオブジェクトを取得します (スレッド セーフ)。 |
| IEnumerable.GetEnumerator() |
コレクションの列挙子を取得します。 |
拡張メソッド
| 名前 | 説明 |
|---|---|
| AsParallel(IEnumerable) |
クエリの並列化を有効にします。 |
| AsQueryable(IEnumerable) |
IEnumerable を IQueryableに変換します。 |
| Cast<TResult>(IEnumerable) |
IEnumerable の要素を指定した型にキャストします。 |
| OfType<TResult>(IEnumerable) |
指定した型に基づいて、IEnumerable の要素をフィルター処理します。 |