pro*_*ick 12 c# data-binding winforms
我是数据绑定的新手.
我有这些课程:
public class Foo : List<Bar>
{
public string FooName { get; set; }
}
public class Bar
{
public string BarName { get; set; }
public string BarDesc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个 List<Foo>
我想要有Foo物品ComboBox和Bar物品ListBox.当我更改所选项目时ComboBox,我想要ListBox更改.当我更改所选项目时,ListBox我想TextBox填写BarDesc.
下面只对作品ListBox和ComboBox:
comboBox1.DataSource = foos;
comboBox1.DisplayMember = "FooName";
listBox1.DataBindings.Add("DataSource", foos, "");
listBox1.DisplayMember = "BarName";
Run Code Online (Sandbox Code Playgroud)
我现在不如何绑定选择做Bar的ListBox到TextBox.Text财产.也许添加绑定listBox1不是一个好主意.
也许我应该这样做:
((CurrencyManager)listBox1.BindingContext[foos]).CurrentChanged += new EventHandler((o, a) =>
{
textBox1.DataBindings.Clear();
textBox1.DataBindings.Add("Text", listBox1.DataSource, "BarDesc");
});
Run Code Online (Sandbox Code Playgroud)
我该如何解决我的问题?
AMi*_*ico 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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23667 次 |
| 最近记录: |