代码之家  ›  专栏  ›  技术社区  ›  prostynick

WinForms数据绑定

  •  12
  • prostynick  · 技术社区  · 16 年前

    我是数据绑定方面的新手。

    我有这些课程:

    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>

    我想要 Foo 项目在 ComboBox Bar 项目在 ListBox . 当我在中更改所选项目时 组合框 我想要 列表框 改变。当我在中更改所选项目时 列表框 我想要 TextBox 充满 BarDesc .

    以下仅适用于 列表框 组合框 :

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

    我现在不知道如何绑定选定的 酒吧 在里面 列表框 TextBox.Text 财产。可能添加绑定 listBox1 不是个好主意。

    也许我应该这样做:

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

    我如何解决我的问题?

    4 回复  |  直到 8 年前
        1
  •  17
  •   AMissico    16 年前

    为了使所有这些都能工作,我必须添加 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");
    
        }
    }
    
        2
  •  0
  •   Stefan    16 年前

    使用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");
    
        3
  •  0
  •   Big Endian    16 年前

    您可能应该在组合框和列表框中设置valuemember。您可能还需要处理bindingsource.currentChanged并将列表框重新绑定到当前选定的列表。

        4
  •  0
  •   Researcher    8 年前

    关于继承和组合的主题外。

    你实际上是在说“foo是一个有名字的酒吧列表”,而说“foo有名字和酒吧列表”要简单得多。但是为什么呢?

    如果你想让foo有两个列表呢?还是一张单子和一本字典?C不允许多重继承(它本身就是脏的!),所以您必须去创建另一个类来保存多个数据结构。

    另外,由于继承的原因,您必须创建一个“items”方法,该方法返回自己不必要的代码。

    试试这个:

    public class Foo
    {
        public string FooName { get; set; }
        public List<Bar> Items;
    }
    
    推荐文章