ComboBox不更新所选项目上的DataBindings更改(WinForms) [英] ComboBox not updating DataBindings on selected item changed (WinForms)

查看:257
本文介绍了ComboBox不更新所选项目上的DataBindings更改(WinForms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox绑定到数据源,但它不会更新绑定,直到控件失去焦点。当选择的项目更改时,如何获取绑定更新?在下面的屏幕截图中,我希望标签立即更新以反映新的选择。





一些代码:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆