Binding,BindingManagerBase, BindingContext的用法

 

 

 
Binding對象:代表某對象屬性值和某控件屬性值之間的簡單綁定。其主要負責將控件的屬性和對象的屬性進行關聯。

BindingManagerBase:管理綁定到相同數據源和數據成員的所有 Binding 對象。這個對象在前面的章節中沒有涉及,但實際上不管是簡單綁定還是複雜綁定中都使用到了這個對象的相應的派生類。

BindingContext對象: 負責管理從Control類繼承的任意對象的 BindingManagerBase對象集合只要發生數據綁定,那在一個FORM中就一定存在一個BindingContext對象。我們可以通過Form對象BindingContext屬性獲得一個BindingContext對象。

這三個對象掌管着數據綁定的主要功能。我們先來看看其關係:

1. Binding對象負責將控件的屬性和對象的屬性關聯起來。對象的屬性值會被自動傳遞個控件的屬性,而控件的屬性值更改後也會直接傳回對象的屬性(雙向綁定)。

2. 在一個WinForm界面中總是會存在多個控件。所以,一般都會有一組Binding對象來管理不同控件中的屬性和相同數據源中屬性的關聯關係。爲了能方便的管理這樣的一組Binding對象,我們使用繼承至BindingManagerBase的子對象進行管理工作。BindingManagerBase有兩個子類:PropertyManager和CurrencyManager.

其中:PropertyManager : 維護對象的屬性與數據綁定控件屬性之間的 Binding。(見簡單綁定的描述)

CurrencyManager : 管理 Binding 對象的列表。管理列表或集合類型的數據源對象。(見覆雜綁定的描述)

無論是PropertyManager還是CurrencyManager總是和一個數據源對象的對應的。也就是說,一個特定的數據源對象(無論是單一對象還是集合類對象)都會有一個對應的BindingManagerBase的子對象

3.對同一窗體而言,通常都會面對多個數據源而不是一個。所以,也就會產生多個PropertyManager或CurrencyManager對象。BindingContext就主要負責管理多個BindingManagerBase的子對象。BindingContext可以通過窗體的BindingContext屬性獲得。它是一個字典的集合。

爲了更好的說明這三類對象之間的關係,請查看下圖。

Binding,BindingManagerBase, <wbr>BindingContext的用法

上面圖例表明了一下幾件事情:

1.當你的窗體中綁定了3個不同的數據源,數據綁定機制就會自動產生三個對應的BindingManagerBase的子對象與數據源對象對應。其實更爲準確的說法是,如果你的窗體綁定了三個不同的對象,那麼就會產生三個獨立的BindingManagerBase的子對象與其對應。至於是產生PropertyManager還是CurrencyManager就要取決與你綁定的數據源是單一對象還是列表(集合)對象了。上圖說明了這一個點,如果是綁定的是單一對象就會產生PorpertyManager,而如果是列表(集合)對象就會產生一個CurrencyManager。

2. PropertyManager主要管理一組相關的Binding對象,而CurrencyManager主要管理着相應的對象集合(列表對象)。兩個對象管理的側重點不同,一個主要管理數據綁定的基礎對象Binding,而一個主要管理數據綁定的後端數據源對象集合。

比如CurrencyManager可以每次從集合對象中獵取一個對象然後將其綁定的到窗體控件中去,它也可以在集合對象中進行導航。或也可以新增新的對象集合中,等等。

3.因爲對於同一窗體而言,可能綁定到多個數據源也就會產生多個“管理者”,而每一個數據源都會對應一個獨立的“管理者”。所以我們可以通過窗體的BindingContext對象獲得某個特定數據源對應的“管理者”。

相關代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DemoTest5
{
public class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public int Age { get; set; }
public Person(string LastName, string FirstName, int Age)
{
this.LastName = LastName;
this.FirstName = FirstName;
this.Age = Age;
}
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoTest5
{
public partial class Form1 : Form
{
private CurrencyManager cm;
public Form1()
{
InitializeComponent();
List<Person> list = new List<Person>();
list.Add(new Person("LastName1", "FirstName1", 30));
list.Add(new Person("LastName2", "FirstName2", 31));
list.Add(new Person("LastName3", "FirstName3", 32));
list.Add(new Person("LastName4", "FirstName4", 33));

this.textBox1.DataBindings.Add("Text", list, "LastName");
this.textBox2.DataBindings.Add("Text", list, "FirstName");
this.textBox3.DataBindings.Add("Text", list, "Age");
this.dataGridView1.DataSource = list;

cm = (CurrencyManager)this.BindingContext[list];
}

private void button1_Click(object sender, EventArgs e)
{
cm.Position--;
}

private void button2_Click(object sender, EventArgs e)
{
cm.Position++;
}
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章