数据绑定不更新(Data binding not updating)

编程入门 行业动态 更新时间:2024-10-05 01:24:31
数据绑定不更新(Data binding not updating)

我创建了一个简单的数据绑定示例(不幸的是我们的系统中有类似的情况)。 我创建了一个时髦的组合框:

public class FunkyComboBox : ComboBox { private object currentValue = null; public FunkyComboBox() { if (LicenseManager.UsageMode == LicenseUsageMode.Runtime) this.Items.Add("Other..."); } protected override void OnSelectedIndexChanged(EventArgs e) { if (!this.Text.StartsWith("Other") && currentValue != this.SelectedItem) { currentValue = this.SelectedItem; BindingManagerBase bindingManager = DataManager; base.OnSelectedIndexChanged(e); } } protected override void OnSelectionChangeCommitted(EventArgs e) { string itemAsStr = this.SelectedItem != null ? SelectedItem.ToString() : ""; if (itemAsStr.StartsWith("Other")) { string newItem = "item" + this.Items.Count; if (!Items.Contains(newItem)) { Items.Add(newItem); } SelectedItem = newItem; } else { OnSelectedIndexChanged(e); //forces a selectedIndexChanged event to be thrown base.OnSelectionChangeCommitted(e); } } }

当您点击其他 (在我们的系统中,它会打开一个表单,您可以在其中查询数据库等)时添加新项目。 然后我有一个简单的数据对象:

public class MyClass { private string value; public string MyData { get{ return value;} set{ this.value = value;} } }

并且绑定到这个对象的两个控件的测试表单(删除了一些设计器代码):

public partial class Form1 : Form { MyClass myObj = new MyClass(); public Form1() { InitializeComponent(); myObj.MyData = "Nothing"; myClassBindingSource.DataSource = myObj; } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.textBox1 = new System.Windows.Forms.TextBox(); this.myClassBindingSource = new System.Windows.Forms.BindingSource(this.components); this.funkyComboBox1 = new DataBindingTests.FunkyComboBox(); ((System.ComponentModel.ISupportInitialize)(this.myClassBindingSource)).BeginInit(); this.SuspendLayout(); // // textBox1 // this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myClassBindingSource, "MyData", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); // // myClassBindingSource // this.myClassBindingSource.DataSource = typeof(DataBindingTests.MyClass); // // funkyComboBox1 // this.funkyComboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.myClassBindingSource, "MyData", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); this.funkyComboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.myClassBindingSource, "MyData", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); // // Form1 // this.Controls.Add(this.textBox1); this.Controls.Add(this.funkyComboBox1); ((System.ComponentModel.ISupportInitialize)(this.myClassBindingSource)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } private FunkyComboBox funkyComboBox1; private System.Windows.Forms.BindingSource myClassBindingSource; private System.Windows.Forms.TextBox textBox1; }

如果您运行此代码并开始使用组合框进行播放,您将注意到编辑框只有在您点击它时才会更改。 每次更改后,将空值设置为我的对象,并清除文本框。 如何在每次更改后设置正确的值?

I created a simple example with data binding (unfortunately we have a similar case in our system). I created a funky combo box:

public class FunkyComboBox : ComboBox { private object currentValue = null; public FunkyComboBox() { if (LicenseManager.UsageMode == LicenseUsageMode.Runtime) this.Items.Add("Other..."); } protected override void OnSelectedIndexChanged(EventArgs e) { if (!this.Text.StartsWith("Other") && currentValue != this.SelectedItem) { currentValue = this.SelectedItem; BindingManagerBase bindingManager = DataManager; base.OnSelectedIndexChanged(e); } } protected override void OnSelectionChangeCommitted(EventArgs e) { string itemAsStr = this.SelectedItem != null ? SelectedItem.ToString() : ""; if (itemAsStr.StartsWith("Other")) { string newItem = "item" + this.Items.Count; if (!Items.Contains(newItem)) { Items.Add(newItem); } SelectedItem = newItem; } else { OnSelectedIndexChanged(e); //forces a selectedIndexChanged event to be thrown base.OnSelectionChangeCommitted(e); } } }

Which adds new items when you click Other (in our system it opens a form where you can query the database, etc). Then I have a simple data object:

public class MyClass { private string value; public string MyData { get{ return value;} set{ this.value = value;} } }

And a test form with two controls bound to this object (some designer code removed):

public partial class Form1 : Form { MyClass myObj = new MyClass(); public Form1() { InitializeComponent(); myObj.MyData = "Nothing"; myClassBindingSource.DataSource = myObj; } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.textBox1 = new System.Windows.Forms.TextBox(); this.myClassBindingSource = new System.Windows.Forms.BindingSource(this.components); this.funkyComboBox1 = new DataBindingTests.FunkyComboBox(); ((System.ComponentModel.ISupportInitialize)(this.myClassBindingSource)).BeginInit(); this.SuspendLayout(); // // textBox1 // this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myClassBindingSource, "MyData", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); // // myClassBindingSource // this.myClassBindingSource.DataSource = typeof(DataBindingTests.MyClass); // // funkyComboBox1 // this.funkyComboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.myClassBindingSource, "MyData", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); this.funkyComboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.myClassBindingSource, "MyData", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); // // Form1 // this.Controls.Add(this.textBox1); this.Controls.Add(this.funkyComboBox1); ((System.ComponentModel.ISupportInitialize)(this.myClassBindingSource)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } private FunkyComboBox funkyComboBox1; private System.Windows.Forms.BindingSource myClassBindingSource; private System.Windows.Forms.TextBox textBox1; }

If you run this code and start playing with the combo box, you will notice that the edit box changes only if you click on it. After every change a null value is set to my object and the text box is cleared. How can I make it set the correct value after every change?

最满意答案

我不确定为什么ComboBox数据绑定的行为如此,但我找到了一种解决方法。 看起来好像数据绑定不能正确工作,如果你不使用你的ComboBox的值的数据源。

我对FunkyComboBox做了一些小改动,现在它按预期工作。

public class FunkyComboBox : ComboBox { private object currentValue = null; private List<string> innerItems = new List<string>(); public FunkyComboBox() { if (LicenseManager.UsageMode == LicenseUsageMode.Runtime) innerItems.Add("Other..."); this.DataSource = innerItems; } protected override void OnSelectedIndexChanged(EventArgs e) { if (!this.Text.StartsWith("Other") && currentValue != this.SelectedItem) { currentValue = this.SelectedItem; BindingManagerBase bindingManager = DataManager; base.OnSelectedIndexChanged(e); } } protected override void OnSelectionChangeCommitted(EventArgs e) { string itemAsStr = this.SelectedItem != null ? SelectedItem.ToString() : ""; if (itemAsStr.StartsWith("Other")) { string newItem = "item" + this.Items.Count; if(!innerItems.Contains(newItem)) { innerItems.Add(newItem); this.RefreshItems(); } SelectedItem = newItem; } else { OnSelectedIndexChanged(e); //forces a selectedIndexChanged event to be thrown base.OnSelectionChangeCommitted(e); } } }

It seems to be a bug with the base ComboBox as well. It is not possible to get this binding source craziness to work correctly.

更多推荐

本文发布于:2023-08-07 13:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   数据   Data   binding   updating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!