更新:我在完全測試之前檢查了答案仍然無效。我更新了下面的代碼,所以你應該能夠粘貼到一個空的WinForms項目中並且它應該可以編譯。ComboBox不會更新其顯示列表,除非您先改變選擇
更新: 我發現如果我將組合框上的選定項目更改爲任何其他項目,現在它的行爲如預期(在我的代碼中,我將從test1切換到test2)。由於我還沒有收到任何答案,我把這個問題改變了。
爲什麼我必須在組合框中更改爲其他項目才能顯示我對底層數據源所做的更改?
這是一個快速測試案例。
- 變化
test1
到test1asdf
文本txtBroken - 點擊關閉提交變化
- 在組合框中的文本不會更新。
- 更改組合框TEST2在txtBroken
- 變化
test2
到test2asdf
文本 - 單擊關閉提交變化
- 在組合框中的文本立即顯示「test2asdf」仍然顯示
test1
在下拉 第一項
- 變化
test1
- 組合框顯示
test1
文本框中顯示test1asdf
- 更新化文本b牛至
test1asd
- 組合框立即顯示比場景改變負載所選擇的項目,改變它的背後其他
test1asd
(這似乎是這樣的黑客)我怎樣才能解決這個問題?
我有一個組合框數據綁定到綁定到List<Holder>
有Holder.Name
作爲其顯示值BindingSource
。我也有一個文本框綁定到Holder.Name
,但如果我改變文本框中的文本,它不會改變組合框中顯示的內容。更改所選項目並更改後將顯示文本框中的更新文本,但仍會在組合框中顯示舊值。如何使組合框中的項目更新?
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Sandbox_Form
{
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
lstBroken = new BindingList<Holder>();
lstBroken.Add(new Holder("test1"));
lstBroken.Add(new Holder("test2"));
bsBroken = new BindingSource(lstBroken, null);
cmbBroken.DataSource = bsBroken;
cmbBroken.DisplayMember = "Name";
cmbBroken.SelectedIndex = 0;
txtBroken.DataBindings.Add("Text", bsBroken, "Name");
txtBroken.TextChanged += new EventHandler(txtBroken_TextChanged);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
void txtBroken_TextChanged(object sender, EventArgs e)
{
((Control)sender).FindForm().Validate();
}
private BindingSource bsBroken;
private BindingList<Holder> lstBroken;
private ComboBox cmbBroken;
private TextBox txtBroken;
private Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmbBroken = new System.Windows.Forms.ComboBox();
this.txtBroken = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// cmbBroken
//
this.cmbBroken.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbBroken.FormattingEnabled = true;
this.cmbBroken.Location = new System.Drawing.Point(12, 32);
this.cmbBroken.Name = "cmbBroken";
this.cmbBroken.Size = new System.Drawing.Size(94, 21);
this.cmbBroken.TabIndex = 0;
//
// txtBroken
//
this.txtBroken.Location = new System.Drawing.Point(13, 60);
this.txtBroken.Name = "txtBroken";
this.txtBroken.Size = new System.Drawing.Size(93, 20);
this.txtBroken.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Broken";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtBroken);
this.Controls.Add(this.cmbBroken);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void cmbWorks_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
public class Holder
{
public Holder(string name)
{
Name = name;
}
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
}
}
}
}
如果我綁定到List<String>
而不是使用Holder.Name
它按預期工作(這只是一個簡單的實體模型,真正的階級已經不僅僅是一個名字更使字符串列表將無法正常工作)。我認爲這是對什麼是錯誤的線索,但我不知道它是什麼。使用Observable而不是列表沒有任何區別。
實際上將txtBackupName datacontext設置爲cboJobSelector的選定項,然後綁定到Name屬性,作爲當前對組合框文本屬性的唯一綁定。據我所知組合框不會更新原始源如果您更改其當前文本屬性。 – 2010-08-25 16:04:36
@LnDCobra我該怎麼做? – 2010-08-25 16:05:22
@Scott我不知道如何在代碼中完成它,我知道如何做到這一點xaml,但我可以問你爲什麼使用綁定與WinForms?而不只是使用WPF? – 2010-08-25 16:08:58