2010-02-12 143 views
13

我是新的數據綁定。WinForms數據綁定

我有這些類:

public class Foo : List<Bar> 
{ 
    public string FooName { get; set; } 
} 

public class Bar 
{ 
    public string BarName { get; set; } 
    public string BarDesc { get; set; } 
} 

而且我有一個List<Foo>

我想在ComboBoxFoo項目,並在ListBoxBar項目。當我更改ComboBox中的所選項目時,我想要ListBox更改。當我更改ListBox中的選定項目時,我希望TextBox填有BarDesc

以下工作只爲ListBoxComboBox

comboBox1.DataSource = foos; 
comboBox1.DisplayMember = "FooName"; 
listBox1.DataBindings.Add("DataSource", foos, ""); 
listBox1.DisplayMember = "BarName"; 

我現在不如何ListBox綁定選擇BarTextBox.Text財產做。也許爲listBox1添加綁定不是一個好主意。

也許我應該做這樣的事情:

((CurrencyManager)listBox1.BindingContext[foos]).CurrentChanged += new EventHandler((o, a) => 
{ 
    textBox1.DataBindings.Clear(); 
    textBox1.DataBindings.Add("Text", listBox1.DataSource, "BarDesc"); 
}); 

我怎樣才能解決我的問題呢?

+0

微軟從來沒有打算讓類繼承'List '。我認爲他們推薦'Collection '作爲基礎班。 – ja72 2015-05-08 12:56:23

+0

我不知道它,但即使我切換到集合問題本來會留下。無論如何,現在我寧願不擴展'List ',而是把它作爲一個屬性聚集在'Foo'中。 – prostynick 2015-05-08 13:08:40

+0

是的,這只是一句話,與問題的解決無關。見http://stackoverflow.com/q/21692193/380384 – ja72 2015-05-08 17:16:46

回答

17

爲了完成所有這些工作,我必須將Items屬性添加到Foo類。這是兩個綁定源之間的「鏈接/關係」。

public partial class Form1 : Form { 
    public class Foo : List<Bar> { 
     public string FooName { get; set; } 
     public Foo(string name) { this.FooName = name; } 
     public List<Bar> Items { get { return this; } } 
    } 
    public class Bar { 
     public string BarName { get; set; } 
     public string BarDesc { get; set; } 
     public Bar(string name, string desc) { 
      this.BarName = name; 
      this.BarDesc = desc; 
     } 
    } 
    public Form1() { 

     InitializeComponent(); 

     List<Foo> foos = new List<Foo>(); 

     Foo a = new Foo("letters"); 
     a.Add(new Bar("a", "aaa")); 
     a.Add(new Bar("b", "bbb")); 
     foos.Add(a); 

     Foo b = new Foo("digits"); 
     b.Add(new Bar("1", "111")); 
     b.Add(new Bar("2", "222")); 
     b.Add(new Bar("3", "333")); 
     foos.Add(b); 

     //Simple Related Object List Binding 
     //http://blogs.msdn.com/bethmassi/archive/2007/04/21/simple-related-object-list-binding.aspx 

     BindingSource comboBoxBindingSource = new BindingSource(); 
     BindingSource listBoxBindingSource = new BindingSource(); 

     comboBoxBindingSource.DataSource = foos; 
     listBoxBindingSource.DataSource = comboBoxBindingSource; 
     listBoxBindingSource.DataMember = "Items"; 

     comboBox1.DataSource = comboBoxBindingSource; 
     comboBox1.DisplayMember = "FooName"; 

     listBox1.DataSource = listBoxBindingSource; 
     listBox1.DisplayMember = "BarName"; 

     textBox1.DataBindings.Add("Text", listBoxBindingSource, "BarDesc"); 

    } 
} 
+1

哦,這很好 '公開名單項目{get {return this; }}' 爲什麼我沒有想過呢? :) – prostynick 2010-03-04 14:13:09

+1

因爲它不是很明顯,你希望/可以通過它的一個屬性返回對象本身,除非你之前運行過它。我敢打賭你會記得這個技術在未來幾年。 :O) – AMissico 2010-03-04 21:33:23

1

使用BindingSource的滿足您的所有複雜的數據綁定需要:

// bind to List<Foo> 
BindingSource comboBoxBindingSource = new BindingSource(); 
comboBoxBindingSource.DataSource = foos; 
// use this binding source in comboBox1 
// for display use FooName 
comboBox1.DataSource = comboBoxBindingSource; 
comboBox1.DisplayMember = "FooName"; 

// bind to comboBox1's SelectedValue 
// it will point to the Foo selected in combobox 
BindingSource listBoxBindingSource = new BindingSource(); 
listBoxBindingSource.DataSource = comboBox1; 
listBoxBindingSource.DataMember = "SelectedValue"; 
// use this binding source in listBox1 
// for display use BarName 
listBox1.DataSource = listBoxBindingSource; 
listBox1.DisplayMember = "BarName"; 

// bind to comboBox'1s SelectedValue (reusing listBoxBindingSource) 
// and set Text to value of BarDesc 
textBox1.DataBindings.Add("Text", listBoxBindingSource, "BarDesc"); 
0

你或許應該設置ValueMember的組合和列表框。您可能還需要處理bindingSource.CurrentChanged並將列表框重新綁定到當前選定的列表。