C# 更新组合框绑定到通用列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/433281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Update combobox bound to generic list
提问by Fueled
I have a combobox on my form that is bound to a generic list of string like this:
我的表单上有一个组合框,它绑定到这样的通用字符串列表:
private List<string> mAllianceList = new List<string>();
private void FillAllianceList()
{
// Add alliance name to member alliance list
foreach (Village alliance in alliances)
{
mAllianceList.Add(alliance.AllianceName);
}
// Bind alliance combobox to alliance list
this.cboAlliances.DataSource = mAllianceList;
}
The user may then add or remove items in the combobox.
I have read elsewhere that by simply adding or removing the item in the generic list, the contents of the combobox should automatically be updated; same thing should occur if I use Sort() on it.
But for some reason, I cannot make this work. I can see the combobox's DataSource property is correctly updated as I add/remove/sort items, but the contents displayed in the combobox are not those in the DataSource property.
然后用户可以在组合框中添加或删除项目。
我在别处读到通过简单地添加或删除通用列表中的项目,组合框的内容应该自动更新;如果我在上面使用 Sort() 也会发生同样的事情。
但由于某种原因,我无法完成这项工作。我可以看到组合框的 DataSource 属性在我添加/删除/排序项目时正确更新,但组合框中显示的内容不是 DataSource 属性中的内容。
I am surely missing something or doing something wrong.
Thanks in advance!
我肯定错过了什么或做错了什么。
提前致谢!
EDIT:
The answer I chose solved the issue for adding and removing, but a BindingList object cannot be sorted, and this is necessary for me. I've found a solution where a custom class is built by inheriting BindingList and adding sorting capabilities, but I would like to know if there's an easier solution in my case.
Any suggestions on how to solve this easily?
编辑:
我选择的答案解决了添加和删除的问题,但无法对 BindingList 对象进行排序,这对我来说是必要的。我找到了一个解决方案,其中通过继承 BindingList 并添加排序功能来构建自定义类,但我想知道在我的情况下是否有更简单的解决方案。
关于如何轻松解决这个问题的任何建议?
采纳答案by BFree
The easiest way around this would be to simply use a BindingList like so:
解决此问题的最简单方法是简单地使用 BindingList,如下所示:
private List<string> mAllianceList = new List<string>();
private BindingList<string> bindingList;
private void FillAllianceList()
{
// Add alliance name to member alliance list
foreach (Village alliance in alliances)
{
mAllianceList.Add(alliance.AllianceName);
}
bindingList = new BindingList<string>(mAllianceList);
// Bind alliance combobox to alliance list
this.cboAlliances.DataSource = bindingList;
}
Then, from here on out, just deal with the binding list to add and remove items from there. That will remove it both from the List and from the ComboBox.
然后,从现在开始,只需处理绑定列表以从那里添加和删除项目。这将从列表和组合框中删除它。
EDIT: To answer your question regarding sorting, I guess the easiest (but possibly "hacky" way to do it would be something like this:
编辑:为了回答你关于排序的问题,我想最简单的(但可能是“hacky”的方法是这样的:
mAllianceList.Sort();
bindingList = new BindingList<string>(mAllianceList);
this.cboAlliances.DataSource = bindingList;
So basically, after you sort, you create a new binding list and reset the data source. Maybe there's a more elegant way to go about this, however this should work.
所以基本上,在你排序之后,你创建一个新的绑定列表并重置数据源。也许有一种更优雅的方法来解决这个问题,但这应该可行。