刷新 C# 和 .NET 4.0 中的 ComboBox 数据绑定
我有一个绑定到列表的组合框(Windows 窗体)。它是在设计时创建的。当列表内容更改时,我的代码调用一个函数来刷新数据绑定。 这适用于 .NET 3.5:
BindingData.SuspendBinding();
DataSource = null;
DataSource = BindingData;
BindingData.ResumeBinding();
我已切换到 .NET 4.0,但它已停止工作。具体来说,在单步执行此代码后,VS 调试器显示 BindingData.DataSource 引用包含 127 个项目的列表,但 ComboBox Items 属性包含零个项目。
请参阅类似主题的SO问题: ComboBox 项目计数与数据源不匹配< /a>.
我已经尝试了我能想到的一切。目前我的代码如下所示,但仍然不起作用:
BindingData.SuspendBinding();
DataSource = null;
DataSource = BindingData;
BindingData.ResumeBinding();
BindingContext Dummy = this.BindingContext;
Invalidate();
PerformLayout();
我尝试从 List 切换到 BindingList,但这没有帮助。我不得不违背自己的意愿从 .NET 3.5 切换到 .NET 4.0,所以这非常令人沮丧。我确信有一个有效的特定顺序。有什么想法吗?
这就是我将数据源附加到组合框的方式:
private BindingSource BindingData = new BindingSource();
BindingData.DataSource = Nodes;
DataSource = BindingData;
谢谢,Andy
I have a ComboBox (Windows Forms) that is bound to a List. It is created at design time. When the List contents are changed my code calls a function to refresh the data binding. This works fine for .NET 3.5:
BindingData.SuspendBinding();
DataSource = null;
DataSource = BindingData;
BindingData.ResumeBinding();
I have switched to .NET 4.0 and it has stopped working. Specifically after stepping through this code the VS debugger shows BindingData.DataSource refers to a list with 127 items, but the ComboBox Items property contains zero items.
See this SO question along a similar theme: ComboBox Items Count Doesn't Match DataSource.
I have tried everything I can think of. Currently my code looks like the following and still doesn't work:
BindingData.SuspendBinding();
DataSource = null;
DataSource = BindingData;
BindingData.ResumeBinding();
BindingContext Dummy = this.BindingContext;
Invalidate();
PerformLayout();
I tried switching from List to BindingList and that didn't help. I had to switch from .NET 3.5 to .NET 4.0 against my will so this is pretty frustrating. I'm sure there is a specific sequence that works. Any ideas?
This is how I am attaching the data source to the ComboBox:
private BindingSource BindingData = new BindingSource();
BindingData.DataSource = Nodes;
DataSource = BindingData;
thanks, Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我解决了。我想在某个时候我做了一些我认为是微小的改变,但实际上却没有。此代码从显示 ComboBox 时调用移至创建 ComboBox 时调用。它还没有句柄,因此无法刷新数据绑定。
我在 ComboBox.HandleCreated 事件中再次添加了数据绑定的另一次刷新,并且它有效。
谢谢,安迪
I solved it. I guess at some point I made what I thought was a minor change but actually wasn't. This code was moved from being called when the ComboBox is being displayed to when it was being created. It didn't yet have a handle and so the data binding cannot be refreshed.
I added another refresh of the databinding again in a ComboBox.HandleCreated event and it works.
thanks, Andy
为什么要暂停并恢复 BindingSource?如果您只是更改数据源,则不会出现性能缺陷。
Why you're suspending and resuming the BindingSource? If you just change your DataSource there will be no performance pitfalls.
根据 如何:将 Windows 窗体组合框或列表框控件绑定到数据 您可以使用组合框的 DisplayMember 属性:
“Length”是指
String< 的公共属性/代码> 类。更好的是直接引用字符串字符的属性。
String
唯一剩余的公共属性是Chars
但我无法让它工作。因此,我们通过comboBox.DisplayMember = ""
重置 DisplayMember,导致 ComboBox 调用每个 List 项的 (aString
)ToString
方法 =>问题解决了。除字符串之外的其他列表条目可以由 ComboBox 的属性
DisplayMember
和ValueMember
处理(它们也适用于其他控件):DisplayMember & ValueMember
According to How to: Bind a Windows Forms ComboBox or ListBox Control to Data you can use the ComboBox's DisplayMember property:
"Length" refers to a public property of the
String
class. Better would be a property that refers directly to the string's characters. The only remaining public property ofString
isChars
but I couldn't make it work. So we reset DisplayMember bycomboBox.DisplayMember = ""
, causing the ComboBox to call each List item's (aString
)ToString
method => problem solved.Other List entries than Strings can be handled by the ComboBox's properties
DisplayMember
andValueMember
(they also apply to other controls):DisplayMember & ValueMember