刷新 C# 和 .NET 4.0 中的 ComboBox 数据绑定

发布于 2024-09-19 11:31:17 字数 1070 浏览 4 评论 0原文

我有一个绑定到列表的组合框(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光无声 2024-09-26 11:31:17

我解决了。我想在某个时候我做了一些我认为是微小的改变,但实际上却没有。此代码从显示 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

孤独陪着我 2024-09-26 11:31:17

为什么要暂停并恢复 BindingSource?如果您只是更改数据源,则不会出现性能缺陷。

Why you're suspending and resuming the BindingSource? If you just change your DataSource there will be no performance pitfalls.

ぇ气 2024-09-26 11:31:17

根据 如何:将 Windows 窗体组合框或列表框控件绑定到数据 您可以使用组合框的 DisplayMember 属性:

//Sample for C++ .NET:
List<String^>^ options = gcnew List<String^>();
options->Add("Option 1");
options->Add("Option 2");

comboBox.DataSource = options;  
comboBox.DisplayMember = "Length";//this causes an DataSource update but the ComboBox would
                                  //show an item's length instead of the item itself
comboBox.DisplayMember = "";      //reset -> the ComboBox calls each List item's ToString
                                  //member

“Length”是指 String< 的公共属性/代码> 类。更好的是直接引用字符串字符的属性。 String 唯一剩余的公共属性是 Chars 但我无法让它工作。因此,我们通过 comboBox.DisplayMember = "" 重置 DisplayMember,导致 ComboBox 调用每个 List 项的 (a String) ToString 方法 =>问题解决了。

除字符串之外的其他列表条目可以由 ComboBox 的属性 DisplayMemberValueMember 处理(它们也适用于其他控件):
DisplayMember & ValueMember

According to How to: Bind a Windows Forms ComboBox or ListBox Control to Data you can use the ComboBox's DisplayMember property:

//Sample for C++ .NET:
List<String^>^ options = gcnew List<String^>();
options->Add("Option 1");
options->Add("Option 2");

comboBox.DataSource = options;  
comboBox.DisplayMember = "Length";//this causes an DataSource update but the ComboBox would
                                  //show an item's length instead of the item itself
comboBox.DisplayMember = "";      //reset -> the ComboBox calls each List item's ToString
                                  //member

"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 of String is Chars but I couldn't make it work. So we reset DisplayMember by comboBox.DisplayMember = "", causing the ComboBox to call each List item's (a String) ToString method => problem solved.

Other List entries than Strings can be handled by the ComboBox's properties DisplayMember and ValueMember (they also apply to other controls):
DisplayMember & ValueMember

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文