【发布时间】:2016-03-14 05:45:25
【问题描述】:
我正在使用 WinFroms 并尝试使用 BindingSource 将控件(ComboBox)更改反映到 DataSource。其实我想看看在comboBox中选择了什么项目。
我的模型是:
public class Foo
{
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
public class Bar
{
public List<Foo> Foos { get; set; }
public Foo SelectedFoo { get; set; }
}
绑定:
List<Foo> lst = new List<Foo>();
lst.Add(new Foo{Name="Name1"});
lst.Add(new Foo{Name="Name2"});
Bar bar = new Bar { Foos = lst };
InitializeComponent();
// bSource - is a BindingSource on the form
this.bSource.DataSource = bar;
// cbBinds - is a ComboBox
this.cbBinds.DataSource = bar.Foos;
this.cbBinds.DataBindings.Add(new Binding("SelectedItem", this.bSource, "Foos", true));
此代码有效,所有 Foo 都显示在 cbBinding 中。但我也想反映组合框中所选项目何时更改。所以我希望 Bar.SelectedFoo 等于 cbBinds.SelectedItem(不使用组合框的更改事件)。
我不知道该怎么做。有可能吗?
【问题讨论】:
标签: c# winforms data-binding combobox bindingsource