簡體   English   中英

WinForms 數據綁定

[英]WinForms data binding

關於數據綁定,我有這些類:

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項目,在ListBox Bar項目。 當我更改ComboBox選定項目時,我希望ListBox更改。 當我更改ListBox選定項目時,我希望TextBox填充BarDesc

以下僅適用於ListBoxComboBox

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

我現在不知道如何將ListBox選定Bar綁定到TextBox.Text屬性。 也許為listBox1添加綁定不是一個好主意。

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

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

我該如何解決我的問題?

為了使所有這些工作,我必須將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");

    }
}

使用 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");

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

關於繼承與組合的題外話。

您實際上是在說“Foo 是一個有名稱的條形列表”,而說“Foo 有一個名稱並且有一個條形列表”會簡單得多。 但為什么?

如果你想讓 Foo 有兩個列表怎么辦? 還是列表和字典? C# 不允許多重繼承(它本身很臟!),因此無論如何您都必須創建另一個包含多個數據結構的類。

同樣由於繼承,您必須創建一個返回自身的“Items”方法,不必要的代碼。

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM