Reputation: 672
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));
}
Upvotes: 7
Views: 20957
Reputation: 81620
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));
Upvotes: 10