我有一个绑定到数据源的组合框,但在控件失去焦点之前它不会更新绑定。当所选项目发生更改时,如何更新绑定?在下面的屏幕截图中,我希望标签立即更新以反映新的选择。
一些代码:
public enum MyEnum
{
First,
Second
}
public class MyData
{
public String Name { get; set; }
public MyEnum MyEnum { get; set; }
}
表格样本:
public SampleForm()
{
InitializeComponent ();
MyData data = new MyData () { Name = "Single Item" };
this.bindingSource1.DataSource = data;
this.comboBox1.DataSource = Enum.GetValues (typeof (MyEnum));
this.label2.DataBindings.Add ("Text", this.bindingSource1, "MyEnum", true, DataSourceUpdateMode.OnPropertyChanged);
this.comboBox1.DataBindings.Add (new System.Windows.Forms.Binding ("SelectedItem", this.bindingSource1, "MyEnum", true));
this.comboBox1.DataBindings.Add (new System.Windows.Forms.Binding ("SelectedValue", this.bindingSource1, "MyEnum", true));
}
注释掉 SelectedItem 版本,并像这样修改 SelectedValue 绑定以包含 UpdateMode:
this.comboBox1.DataBindings.Add(new Binding(
"SelectedValue",
this.bindingSource1,
"MyEnum",
true,
DataSourceUpdateMode.OnPropertyChanged));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)