簡體   English   中英

ComboBox沒有更新所選項目上的DataBindings更改(WinForms)

[英]ComboBox not updating DataBindings on selected item changed (WinForms)

我有一個綁定到數據源的ComboBox但它不會更新綁定,直到控件失去焦點。 如何在所選項目更改時獲取更新綁定? 在下面的屏幕截圖中,我希望標簽立即更新以反映新的選擇。

一些代碼:

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));

LarsTech解決方案是正確的。 您也可以在設計模式下執行此操作:

  1. ComboBox屬性(F4) - > DataBindings節點 - >高級

  1. 單擊“SelectedValue”並將“數據源更新模式”更改為“OnPropertyChanged” 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM