BindingContext BindingContext BindingContext BindingContext Class

Definizione

Gestisce la raccolta di oggetti BindingManagerBase per qualsiasi oggetto che eredita dalla classe Control.Manages the collection of BindingManagerBase objects for any object that inherits from the Control class.

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
Ereditarietà
BindingContextBindingContextBindingContextBindingContext
Implementazioni

Esempi

L'esempio di codice seguente crea quattro Binding oggetti cinque controlli - associare un DateTimePicker e quattro TextBox - controlli su diverse origini dati.The following code example creates four Binding objects to bind five controls - a DateTimePicker and four TextBox controls - to several data sources. Il BindingContext viene quindi usato per ottenere il BindingManagerBase per ogni origine dati.The BindingContext is then used to get the BindingManagerBase for each data source.

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

Commenti

Ogni modulo di Windows ha almeno una BindingContext oggetto che gestisce il BindingManagerBase oggetti per il form.Each Windows Form has at least one BindingContext object that manages the BindingManagerBase objects for the form. Poiché il BindingManagerBase classe è astratta, il tipo restituito del Item[Object] proprietà è un CurrencyManager o un PropertyManager.Because the BindingManagerBase class is abstract, the return type of the Item[Object] property is either a CurrencyManager or a PropertyManager. Se l'origine dati è un oggetto che può restituire solo una singola proprietà (anziché un elenco di oggetti), il Type è un PropertyManager.If the data source is an object that can return only a single property (instead of a list of objects), the Type is a PropertyManager. Ad esempio, se si specifica un TextBox come origine dati, un PropertyManager viene restituito.For example, if you specify a TextBox as the data source, a PropertyManager is returned. D'altra parte, se l'origine dati è un oggetto che implementa IList oppure IBindingList, un CurrencyManager viene restituito.On the other hand, if the data source is an object that implements IList or IBindingList, a CurrencyManager is returned.

Per ogni origine dati in un Form di Windows, è presente una sola CurrencyManager o PropertyManager.For each data source on a Windows Form, there is a single CurrencyManager or PropertyManager. Poiché possono esistere più origini di dati associate a un Form di Windows, il BindingContext consente di recuperare qualsiasi particolare CurrencyManager associata a un'origine dati.Because there may be multiple data sources associated with a Windows Form, the BindingContext enables you to retrieve any particular CurrencyManager associated with a data source.

Nota

Quando si usa la Item[Object] proprietà, il BindingContext crea un nuovo BindingManagerBase se non ne esiste già.When using the Item[Object] property, the BindingContext creates a new BindingManagerBase if one does not already exist. Ciò può causare confusione, poiché l'oggetto restituito non può gestire l'elenco (o qualsiasi elenco) che si intende.This can lead to some confusion, as the returned object may not manage the list (or any list) that you intend. Per evitare di restituire un valore non valido BindingManagerBase, usare il Contains metodo per determinare se il destinatario BindingManagerBase esiste già.To prevent returning an invalid BindingManagerBase, use the Contains method to determine if the intended BindingManagerBase already exists.

Se si usa un controllo contenitore, ad esempio un GroupBox, Panel, o TabControlper contenere i controlli con associazione a dati, è possibile creare un BindingContext solo per tale contenitore e i relativi controlli.If you use a container control, such as a GroupBox, Panel, or TabControl, to contain data-bound controls, you can create a BindingContext for just that container control and its controls. Quindi, ogni parte del modulo possa essere gestiti dalla relativa BindingManagerBase.Then, each part of your form can be managed by its own BindingManagerBase. Vedere le BindingContext costruttore per altre informazioni sulla creazione di più BindingManagerBase gli oggetti per la stessa origine dati.See the BindingContext constructor for more information about creating multiple BindingManagerBase objects for the same data source.

Se si aggiunge un TextBox controllo a un form e l'associazione a una colonna di una tabella in un set di dati, il controllo con cui comunica il BindingContext del modulo.If you add a TextBox control to a form and bind it to a column of a table in a dataset, the control communicates with the BindingContext of that form. Il BindingContext, a sua volta comunica con l'oggetto specifico CurrencyManager per quell'associazione dati.The BindingContext, in turn, talks to the specific CurrencyManager for that data association. Se si esegue una query di Position proprietà del CurrencyManager, segnalerebbe del record corrente per l'associazione di tale TextBox controllo.If you queried the Position property of the CurrencyManager, it would report the current record for the binding of that TextBox control. Nell'esempio di codice seguente, un TextBox è associato al FirstName colonna di una Customers tabella sul dataSet1 set di dati tramite la BindingContext per il form a cui appartiene.In the following code example, a TextBox control is bound to the FirstName column of a Customers table on the dataSet1 dataset through the BindingContext for the form it is on.

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

È possibile aggiungere una seconda TextBox controllo (TextBox2) al form e associarlo al LastName colonna del Customers tabella nel set di dati stesso.You can add a second TextBox control (TextBox2) to the form and bind it to the LastName column of the Customers table in the same dataset. Il BindingContext è compatibile con la prima associazione (TextBox1 al Customers.FirstName), quindi viene utilizzato lo stesso CurrencyManager, mentre entrambe le caselle di testo sono associate allo stesso set di dati (DataSet1).The BindingContext is aware of the first binding (TextBox1 to Customers.FirstName), so it would use the same CurrencyManager, as both text boxes are bound to the same dataset (DataSet1).

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

Se si associa TextBox2 a un set di dati diverso, il BindingContext crea e gestisce una seconda CurrencyManager.If you bind TextBox2 to a different dataset, the BindingContext creates and manages a second CurrencyManager.

È importante essere coerente sulla modalità di impostazione la DataSource e DisplayMember delle proprietà; in caso contrario, il BindingContext crea valuta più strumenti di gestione per il set di dati stesso, causando errori.It is important to be consistent about how you set the DataSource and DisplayMember properties; otherwise, the BindingContext creates multiple currency managers for the same dataset, which results in errors. Esempio di codice seguente illustra alcuni modi per impostare le proprietà e i relativi BindingContext oggetti.The following code example shows a few ways to set the properties and their associated BindingContext objects. È possibile impostare le proprietà usando uno dei metodi seguenti, purché siano coerenti in tutto il codice.You can set the properties using either of the following methods, as long as you are consistent throughout your code.

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;  

Nota

La maggior parte delle applicazioni di Windows Form eseguire l'associazione tramite un BindingSource.Most Windows Forms applications bind through a BindingSource. Il BindingSource componente incapsula un' CurrencyManager ed espone il CurrencyManager interfaccia di programmazione.The BindingSource component encapsulates a CurrencyManager and exposes the CurrencyManager programming interface. Quando si usa un BindingSource per l'associazione, è consigliabile usare i membri esposti dal BindingSource modificare "valuta" (vale a dire Position) anziché passare attraverso il BindingContext.When using a BindingSource for binding, you should use the members exposed by the BindingSource to manipulate "currency" (that is, Position) rather than go through the BindingContext.

Costruttori

BindingContext() BindingContext() BindingContext() BindingContext()

Inizializza una nuova istanza della classe BindingContext.Initializes a new instance of the BindingContext class.

Proprietà

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

Ottiene un valore che indica se la raccolta è di sola lettura.Gets a value indicating whether the collection is read-only.

Item[Object, String] Item[Object, String] Item[Object, String] Item[Object, String]

Ottiene l'oggetto BindingManagerBase associato all'origine e al membro dati specificati.Gets a BindingManagerBase that is associated with the specified data source and data member.

Item[Object] Item[Object] Item[Object] Item[Object]

Ottiene l'oggetto BindingManagerBase associato all'origine dati specificata.Gets the BindingManagerBase that is associated with the specified data source.

Metodi

Add(Object, BindingManagerBase) Add(Object, BindingManagerBase) Add(Object, BindingManagerBase) Add(Object, BindingManagerBase)

Aggiunge all'insieme l'oggetto BindingManagerBase associato a una specifica origine dati.Adds the BindingManagerBase associated with a specific data source to the collection.

AddCore(Object, BindingManagerBase) AddCore(Object, BindingManagerBase) AddCore(Object, BindingManagerBase) AddCore(Object, BindingManagerBase)

Aggiunge all'insieme l'oggetto BindingManagerBase associato a una specifica origine dati.Adds the BindingManagerBase associated with a specific data source to the collection.

Clear() Clear() Clear() Clear()

Cancella dall'insieme qualunque oggetto BindingManagerBase.Clears the collection of any BindingManagerBase objects.

ClearCore() ClearCore() ClearCore() ClearCore()

Cancella la raccolta.Clears the collection.

Contains(Object) Contains(Object) Contains(Object) Contains(Object)

Ottiene un valore che indica se BindingContext contiene l'oggetto BindingManagerBase associato all'origine dati specificata.Gets a value indicating whether the BindingContext contains the BindingManagerBase associated with the specified data source.

Contains(Object, String) Contains(Object, String) Contains(Object, String) Contains(Object, String)

Ottiene un valore che indica se BindingContext contiene l'oggetto BindingManagerBase associato all'origine e al membro dati specificati.Gets a value indicating whether the BindingContext contains the BindingManagerBase associated with the specified data source and data member.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs)

Genera l'evento CollectionChanged.Raises the CollectionChanged event.

Remove(Object) Remove(Object) Remove(Object) Remove(Object)

Elimina l'oggetto BindingManagerBase associato all'origine dati specificata.Deletes the BindingManagerBase associated with the specified data source.

RemoveCore(Object) RemoveCore(Object) RemoveCore(Object) RemoveCore(Object)

Rimuove l'oggetto BindingManagerBase associato all'origine dati specificata.Removes the BindingManagerBase associated with the specified data source.

ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)
UpdateBinding(BindingContext, Binding) UpdateBinding(BindingContext, Binding) UpdateBinding(BindingContext, Binding) UpdateBinding(BindingContext, Binding)

Associa un oggetto Binding a un nuovo oggetto BindingContext.Associates a Binding with a new BindingContext.

Eventi

CollectionChanged CollectionChanged CollectionChanged CollectionChanged

Genera sempre un oggetto NotImplementedException durante la gestione.Always raises a NotImplementedException when handled.

Implementazioni dell'interfaccia esplicita

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

Copia gli elementi dell’insieme in una matrice specificata, a partire dall'indice dell'insieme.Copies the elements of the collection into a specified array, starting at the collection index.

ICollection.Count ICollection.Count ICollection.Count ICollection.Count

Ottiene il numero complessivo di oggetti CurrencyManager gestiti da BindingContext.Gets the total number of CurrencyManager objects managed by the BindingContext.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

Ottiene un valore che indica se l'insieme è sincronizzato.Gets a value indicating whether the collection is synchronized.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

Ottiene un oggetto da utilizzare per la sincronizzazione (thread-safe).Gets an object to use for synchronization (thread safety).

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

Ottiene un enumeratore per la raccolta.Gets an enumerator for the collection.

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Esegue il cast di elementi di un IEnumerable nel tipo specificato.Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filtra gli elementi di un IEnumerable basato su un tipo specificato.Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Consente la parallelizzazione di una query.Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converte un' IEnumerable a un IQueryable.Converts an IEnumerable to an IQueryable.

Si applica a

Vedi anche