简体   繁体   English

C#更新绑定到通用列表的组合框

[英]C# Update combobox bound to generic list

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. 如果我对它使用Sort(),应该会发生同样的事情。
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. 我可以看到组合框的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. 我选择的答案解决了添加和删除的问题,但是BindingList对象无法排序,这对我来说是必要的。 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. 我找到了一个解决方案,通过继承BindingList和添加排序功能来构建自定义类,但我想知道在我的情况下是否有更简单的解决方案。
Any suggestions on how to solve this easily? 关于如何轻松解决这个问题的任何建议?

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. 这将从List和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. 也许有更优雅的方式来解决这个问题,但这应该有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM