BindingContext Klasa

Definicja

Zarządza kolekcją BindingManagerBase obiektów dla dowolnego obiektu dziedziczonego Control z klasy.

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
Dziedziczenie
BindingContext
Implementuje

Przykłady

Poniższy przykład kodu tworzy cztery Binding obiekty, aby powiązać pięć kontrolek — kontrolki i cztery TextBox — z kilkoma DateTimePicker źródłami danych. Element BindingContext jest następnie używany do pobierania BindingManagerBase elementu dla każdego źródła danych.

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

Uwagi

Każdy formularz systemu Windows ma co najmniej jeden BindingContext obiekt, który zarządza BindingManagerBase obiektami formularza. Ponieważ klasa jest abstrakcyjna BindingManagerBase , zwracany typ Item[] właściwości to a CurrencyManager lub PropertyManager. Jeśli źródło danych jest obiektem, który może zwrócić tylko jedną właściwość (zamiast listy obiektów), Type element jest .PropertyManager Jeśli na przykład określisz TextBox jako źródło danych, PropertyManager zwracany jest element . Z drugiej strony, jeśli źródło danych jest obiektem, który implementuje IList element lub IBindingList, CurrencyManager jest zwracany.

Dla każdego źródła danych w formularzu systemu Windows istnieje jeden CurrencyManager element lub PropertyManager. Ze względu na to, że może istnieć wiele źródeł danych skojarzonych z formularzem systemu Windows, BindingContext program umożliwia pobranie dowolnego określonego CurrencyManager źródła danych skojarzonego ze źródłem danych.

Uwaga

W przypadku korzystania z Item[] właściwości metoda tworzy nowąBindingManagerBase, BindingContext jeśli jeszcze nie istnieje. Może to prowadzić do pomyłek, ponieważ zwrócony obiekt może nie zarządzać listą (lub dowolną listą), którą zamierzasz. Aby zapobiec zwracaniu nieprawidłowego BindingManagerBaseelementu , użyj Contains metody , aby określić, czy zamierzony BindingManagerBase już istnieje.

Jeśli używasz kontrolki kontenera, takiej jak GroupBox, Panellub TabControl, do przechowywania kontrolek powiązanych z danymi, możesz utworzyć BindingContext obiekt dla tylko tej kontrolki kontenera i jej kontrolek. Następnie każda część formularza może być zarządzana własnymi BindingManagerBaseelementami . Zobacz konstruktor, BindingContext aby uzyskać więcej informacji na temat tworzenia wielu BindingManagerBase obiektów dla tego samego źródła danych.

Jeśli dodasz kontrolkę TextBox do formularza i powiążesz ją z kolumną tabeli w zestawie danych, kontrolka komunikuje się z BindingContext formularzem. Z kolei element BindingContext, komunikuje się ze specyficznym CurrencyManager dla tego skojarzenia danych. Jeśli wykonano zapytanie dotyczące Position właściwości CurrencyManager, raportuje bieżący rekord dla powiązania tej TextBox kontrolki. W poniższym przykładzie kodu kontrolka TextBox jest powiązana z FirstName kolumną Customers tabeli w dataSet1 zestawie danych za pośrednictwem BindingContext formularza, na który się znajduje.

TextBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName")  
textBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName");  
textBox1->DataBindings->Add("Text", dataSet1, "Customers.FirstName");  

Możesz dodać drugą TextBox kontrolkę (TextBox2) do formularza i powiązać ją z kolumną LastNameCustomers tabeli w tym samym zestawie danych. Funkcja BindingContext jest świadoma pierwszego powiązania (TextBox1 do Customers.FirstName), więc będzie używać tego samego CurrencyManagerelementu , co oba pola tekstowe są powiązane z tym samym zestawem danych (DataSet1).

TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")  
textBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName");  
textBox2->DataBindings->Add("Text", dataSet1, "Customers.LastName");  

Jeśli powiążesz z TextBox2 innym zestawem danych, BindingContext program tworzy drugi CurrencyManagerzestaw danych i zarządza nim.

Ważne jest, aby zachować spójność sposobu ustawiania DataSource właściwości i DisplayMember . W przeciwnym razie BindingContext tworzy wiele menedżerów walut dla tego samego zestawu danych, co powoduje błędy. Poniższy przykład kodu przedstawia kilka sposobów ustawiania właściwości i skojarzonych z BindingContext nimi obiektów. Właściwości można ustawić przy użyciu jednej z następujących metod, o ile cały kod jest spójny.

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;  
comboBox1->DisplayMember = "Customers.FirstName";  
this->BindingContext->get_Item(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;  
comboBox1->DataSource = dataSet1->Customers;  
comboBox1->DisplayMember = "FirstName";  
this->BindingContext->get_Item(dataSet1->Customers)->Position = 1;  

Uwaga

Większość aplikacji Windows Forms wiąże się za pomocą klasy BindingSource. Składnik BindingSource hermetyzuje element CurrencyManager i uwidacznia CurrencyManager interfejs programowania. W przypadku użycia BindingSource elementu dla powiązania należy użyć elementów członkowskich uwidocznionych przez BindingSource element , aby manipulować "walutą" (czyli Position) zamiast przechodzić przez element BindingContext.

Konstruktory

BindingContext()

Inicjuje nowe wystąpienie klasy BindingContext.

Właściwości

IsReadOnly

Pobiera wartość wskazującą, czy kolekcja jest przeznaczona tylko do odczytu.

Item[Object, String]

Pobiera element BindingManagerBase skojarzony z określonym źródłem danych i elementem członkowskim danych.

Item[Object]

Pobiera element BindingManagerBase skojarzony z określonym źródłem danych.

Metody

Add(Object, BindingManagerBase)

BindingManagerBase Dodaje element skojarzony z określonym źródłem danych do kolekcji.

AddCore(Object, BindingManagerBase)

BindingManagerBase Dodaje element skojarzony z określonym źródłem danych do kolekcji.

Clear()

Czyści kolekcję wszystkich BindingManagerBase obiektów.

ClearCore()

Czyści kolekcję.

Contains(Object)

Pobiera wartość wskazującą, czy BindingContext element zawiera BindingManagerBase skojarzony z określonym źródłem danych.

Contains(Object, String)

Pobiera wartość wskazującą, czy BindingContext element zawiera BindingManagerBase element skojarzony z określonym źródłem danych i elementem członkowskim danych.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
OnCollectionChanged(CollectionChangeEventArgs)

CollectionChanged Zgłasza zdarzenie.

Remove(Object)

Usuwa BindingManagerBase obiekt skojarzony z określonym źródłem danych.

RemoveCore(Object)

Usuwa BindingManagerBase element skojarzony z określonym źródłem danych.

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)
UpdateBinding(BindingContext, Binding)

Kojarzy element Binding z nowym BindingContextelementem .

Zdarzenia

CollectionChanged

Zawsze podnosi wartość NotImplementedException w przypadku obsługi.

Jawne implementacje interfejsu

ICollection.CopyTo(Array, Int32)

Kopiuje elementy kolekcji do określonej tablicy, zaczynając od indeksu kolekcji.

ICollection.Count

Pobiera całkowitą liczbę CurrencyManager obiektów zarządzanych przez obiekt BindingContext.

ICollection.IsSynchronized

Pobiera wartość wskazującą, czy kolekcja jest zsynchronizowana.

ICollection.SyncRoot

Pobiera obiekt do użycia na potrzeby synchronizacji (bezpieczeństwo wątków).

IEnumerable.GetEnumerator()

Pobiera moduł wyliczający dla kolekcji.

Metody rozszerzania

Cast<TResult>(IEnumerable)

Rzutuje elementy obiektu IEnumerable na określony typ.

OfType<TResult>(IEnumerable)

Filtruje elementy IEnumerable elementu na podstawie określonego typu.

AsParallel(IEnumerable)

Umożliwia równoległość zapytania.

AsQueryable(IEnumerable)

Konwertuje element IEnumerable na .IQueryable

Dotyczy

Zobacz też