ComboBox不更新所选项目上的DataBindings更改(WinForms) [英] ComboBox not updating DataBindings on selected item changed (WinForms)
本文介绍了ComboBox不更新所选项目上的DataBindings更改(WinForms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一些代码:
public enum MyEnum
{
首先,
Second
}
public class MyData
{
public String Name {get;组; }
public MyEnum MyEnum {get;组; }
}
样本表单:
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));
I have a ComboBox bound to a data source but it will not update the bindings until the control loses focus. How can I get the bindings to update when the selected items change? In the screen shot below I'd like the label to updated immediately to reflect the new selection.
Some Code:
public enum MyEnum
{
First,
Second
}
public class MyData
{
public String Name { get; set; }
public MyEnum MyEnum { get; set; }
}
Sample Form:
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));
}
解决方案
Comment out the SelectedItem version, and modify the SelectedValue binding like this to include the UpdateMode:
this.comboBox1.DataBindings.Add(new Binding(
"SelectedValue",
this.bindingSource1,
"MyEnum",
true,
DataSourceUpdateMode.OnPropertyChanged));
这篇关于ComboBox不更新所选项目上的DataBindings更改(WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文