WinForms 数据绑定 [英] WinForms data binding

查看:14
本文介绍了WinForms 数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于数据绑定,我有这些类:

Concerning data binding I have these classes:

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

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

我有一个 List

我希望在 ComboBox 中有 Foo 项,在 ListBox 中有 Bar 项.当我更改 ComboBox 中的选定项目时,我希望 ListBox 更改.当我更改 ListBox 中的选定项目时,我希望 TextBox 填充 BarDesc.

I would like to have Foo items in ComboBox, and Bar items in ListBox. When I change selected item in ComboBox, I want ListBox to change. When I change selected item in ListBox I would like to have TextBox filled with BarDesc.

以下仅适用于 ListBoxComboBox:

Following works only for ListBox and ComboBox:

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

我现在不知道如何将 ListBox 中选定的 Bar 绑定到 TextBox.Text 属性.也许为 listBox1 添加绑定不是一个好主意.

I don't now how to bind selected Bar in ListBox to TextBox.Text property. Maybe added binding for listBox1 is not a good idea.

也许我应该这样做:

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

我该如何解决我的问题?

How can I resolve my problem?

推荐答案

为了使所有这些工作正常进行,我必须将 Items 属性添加到 Foo 类.这是两个绑定源之间的链接/关系".

To make all this work, I had to add the Items property to the Foo class. This is the "link/relationship" between the two binding sources.

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

    }
}

这篇关于WinForms 数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆