BindingContext Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Spravuje kolekci BindingManagerBase objektů pro libovolný objekt, který dědí z Control třídy.
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
- Dědičnost
-
BindingContext
- Implementuje
Příklady
Následující příklad kódu vytvoří čtyři Binding objekty pro vytvoření vazby pěti ovládacích prvků – DateTimePicker a čtyř TextBox ovládacích prvků – k několika zdrojům dat. Ten BindingContext se pak použije k získání BindingManagerBase pro každý zdroj dat.
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
Poznámky
Každý formulář Windows má alespoň jeden objekt BindingContext, který spravuje objekty BindingManagerBase formuláře. BindingManagerBase Vzhledem k tomu, že třída je abstraktní, návratový Item[] typ vlastnosti je buď a CurrencyManager nebo a PropertyManager. Pokud je zdrojem dat objekt, který může vrátit pouze jednu vlastnost (místo seznamu objektů), je to TypePropertyManager. Pokud například zadáte TextBox jako zdroj dat, PropertyManager vrátí se hodnota. Na druhé straně, pokud je zdrojem dat objekt, který implementuje IList nebo IBindingList, CurrencyManager je vrácena.
Pro každý zdroj dat ve formuláři Windows existuje jeden CurrencyManager nebo PropertyManager. Vzhledem k tomu, že formulář Windows může mít více zdrojů dat, BindingContext umožňuje načíst všechny konkrétní CurrencyManager přidružené ke zdroji dat.
Note
Při použití Item[] vlastnosti vytvoří BindingContext novou BindingManagerBase , pokud ještě neexistuje. To může vést k nejasnostem, protože vrácený objekt nemusí spravovat seznam (ani žádný seznam), který máte v úmyslu. Chcete-li zabránit vrácení neplatné BindingManagerBase, použijte metodu Contains k určení, zda zamýšlený BindingManagerBase již existuje.
Pokud k zahrnutí ovládacích prvků vázaných na data používáte ovládací prvek kontejneru, jako GroupBoxje například , PanelTabControlnebo , můžete vytvořit BindingContext ovládací prvek kontejneru a jeho ovládací prvky. Každá část formuláře pak může být spravována vlastním BindingManagerBase. BindingContext Další informace o vytváření více BindingManagerBase objektů pro stejný zdroj dat najdete v konstruktoru.
Pokud do formuláře přidáte TextBox ovládací prvek a svážete ho se sloupcem tabulky v datové sadě, bude ovládací prvek komunikovat s tímto formulářem BindingContext . Naopak BindingContexthovoří o konkrétním CurrencyManager přidružení dat. Pokud jste na vlastnost CurrencyManagerovládacího prvku dotazovaliPosition, nahlásí aktuální záznam pro vazbu tohoto TextBox ovládacího prvku. V následujícím příkladu TextBox kódu je ovládací prvek svázán se FirstName sloupcem Customers tabulky v dataSet1 datové sadě prostřednictvím BindingContext formuláře, na který se nachází.
TextBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName")
textBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName");
Do formuláře můžete přidat druhý TextBox ovládací prvek (TextBox2) a svázat ho Customers se LastName sloupcem tabulky ve stejné datové sadě. Je BindingContext si vědom první vazby (TextBox1 k Customers.FirstName), takže by používala stejné CurrencyManager, jako obě textová pole jsou svázaná se stejnou datovou sadou (DataSet1).
TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")
textBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName");
Pokud svážete vazbu TextBox2 na jinou datovou sadu, BindingContext vytvoří a spravuje sekundu CurrencyManager.
Je důležité mít konzistentní informace o tom, jak nastavíte DataSource a DisplayMember jaké vlastnosti nastavíte. V opačném případě BindingContext vytvoří více správců měn pro stejnou datovou sadu, což vede k chybám. Následující příklad kódu ukazuje několik způsobů, jak nastavit vlastnosti a jejich přidružené BindingContext objekty. Vlastnosti můžete nastavit pomocí některé z následujících metod, pokud budete konzistentní v celém kódu.
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
Většina model Windows Forms aplikací se sváže prostřednictvím BindingSource. Komponenta BindingSource zapouzdřuje CurrencyManager programovací rozhraní a zveřejňuje ho CurrencyManager . Při použití vazby BindingSource byste měli použít členy vystavené BindingSource manipulací s "měnou" (tj Position. ) místo procházení BindingContext.
Konstruktory
| Name | Description |
|---|---|
| BindingContext() |
Inicializuje novou instanci BindingContext třídy. |
Vlastnosti
| Name | Description |
|---|---|
| IsReadOnly |
Získá hodnotu určující, zda kolekce je jen pro čtení. |
| Item[Object, String] |
BindingManagerBase Získá, který je přidružen k zadanému zdroji dat a členu dat. |
| Item[Object] |
BindingManagerBase Získá, který je přidružen k zadanému zdroji dat. |
Metody
| Name | Description |
|---|---|
| Add(Object, BindingManagerBase) |
BindingManagerBase Přidá do kolekce přidružený ke konkrétnímu zdroji dat. |
| AddCore(Object, BindingManagerBase) |
BindingManagerBase Přidá do kolekce přidružený ke konkrétnímu zdroji dat. |
| Clear() |
Vymaže kolekci všech BindingManagerBase objektů. |
| ClearCore() |
Vymaže kolekci. |
| Contains(Object, String) |
Získá hodnotu určující, zda BindingContext obsahuje BindingManagerBase přidružené k zadanému zdroji dat a členu dat. |
| Contains(Object) |
Získá hodnotu určující, zda BindingContext obsahuje přidružen BindingManagerBase k zadanému zdroji dat. |
| Equals(Object) |
Určí, zda se zadaný objekt rovná aktuálnímu objektu. (Zděděno od Object) |
| GetHashCode() |
Slouží jako výchozí funkce hash. (Zděděno od Object) |
| GetType() |
Získá Type aktuální instance. (Zděděno od Object) |
| MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Object. (Zděděno od Object) |
| OnCollectionChanged(CollectionChangeEventArgs) |
CollectionChanged Vyvolá událost. |
| Remove(Object) |
Odstraní přidružený k BindingManagerBase zadanému zdroji dat. |
| RemoveCore(Object) |
Odebere přidruženou BindingManagerBase k zadanému zdroji dat. |
| ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
| UpdateBinding(BindingContext, Binding) |
Přidruží novou Binding .BindingContext |
Událost
| Name | Description |
|---|---|
| CollectionChanged |
Vždy vyvolá NotImplementedException při zpracování. |
Explicitní implementace rozhraní
| Name | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
Zkopíruje prvky kolekce do zadaného pole počínaje indexem kolekce. |
| ICollection.Count |
Získá celkový počet objektů spravovaných CurrencyManager objekty BindingContext. |
| ICollection.IsSynchronized |
Získá hodnotu označující, zda je kolekce synchronizována. |
| ICollection.SyncRoot |
Získá objekt, který se má použít pro synchronizaci (zabezpečení vlákna). |
| IEnumerable.GetEnumerator() |
Získá enumerátor pro kolekci. |
Metody rozšíření
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
Umožňuje paralelizaci dotazu. |
| AsQueryable(IEnumerable) |
Převede IEnumerable na IQueryable. |
| Cast<TResult>(IEnumerable) |
Přetypuje prvky IEnumerable na zadaný typ. |
| OfType<TResult>(IEnumerable) |
Filtruje prvky IEnumerable na základě zadaného typu. |