WinForms数据绑定
我是数据绑定的新手。
我有这些课程:
public class Foo : List { 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
。
以下仅适用于ListBox
和ComboBox
:
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 { public string FooName { get; set; } public Foo(string name) { this.FooName = name; } public List 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 foos = new List (); 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 //https://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 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并将列表框重新绑定到当前选定的列表。
关于inheritance与组成的主题。
你实际上是在说“Foo是一个有免费精选名字大全的条形列表”,当说“Foo有一个免费精选名字大全并有一个条形列表”会更简单。 但为什么?
如果你想让Foo有两个列表怎么办? 还是列表和字典? C#不允许多重inheritance(它本身很脏!),所以你必须去创建另一个包含多个数据结构的类。
此外,由于inheritance,您必须创建一个返回自身的“Items”方法,这是不必要的代码。
试试这个:
上述就是C#学习教程:WinForms数据绑定分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public class Foo { public string FooName { get; set; } public List Items; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/961628.html