简体   繁体   English

将对象添加到绑定列表时,ComboBox不更新

[英]ComboBox not updating when object added to bound list

I have an object that represents a client, and that object has a list of the clients branches: 我有一个代表客户端的对象,并且该对象具有客户端分支的列表:

private List<Branch> _branches;
[System.Xml.Serialization.XmlArray("Branches"), System.Xml.Serialization.XmlArrayItem(typeof(Branch))]
public List<Branch> Branches
{
    get { return _branches; }
    set
    {
        _branches = value;
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs("Branches"));
        }
    }
}

In one form (WinForms) I have a ComboBox that I've bound to that list: 在一种形式(WinForms)中,我有一个绑定到该列表的ComboBox:

// creating a binding list for the branches
var bindingList = new BindingList<Branch>(Client.Branches);

// bind list to combo box
cmbBranches.DataSource = bindingList;
cmbBranches.DisplayMember = "Name";
cmbBranches.ValueMember = "Name";

In another function, I create a new Branch object and add it to the existing list: Client.Branches.Add(newBranch) . 在另一个函数中,我创建一个新的Branch对象并将其添加到现有列表中: Client.Branches.Add(newBranch) I would expect this to update the ComboBox but it doesn't. 我希望这会更新ComboBox,但不会。 Why not, and how do I make it update? 为什么不呢,我该如何更新呢? (Edit: I'd also like this to update when removing an object from the list. The reason it doesn't work is, I assume, directly related to why the box isn't updating when Add is called.) (编辑:当我从列表中删除一个对象时,我也希望此更新。我认为它不起作用的原因与调用Add时该框未更新的原因直接相关。)

In doing research, I found this SO answer, which seems to imply that it will work. 在进行研究时,我找到了这样的答案,这似乎暗示它会起作用。 I feel like I'm missing something simple ... 我觉得我缺少一些简单的东西...

difference between ObservableCollection and BindingList ObservableCollection和BindingList之间的区别

Edit: Some further information about what I've tried and some additional goals. 编辑:有关我尝试过的内容和一些其他目标的更多信息。

I cannot use ObservableCollection<T> instead of List<T> as I need to use Exists in the code. 我不能使用ObservableCollection<T>代替List<T>因为我需要在代码中使用Exists Which the former doesn't have. 前者没有。

I need to update the original list when the new object is added, in addition to updating the drop down box. 添加新对象时,除了更新下拉框之外,我还需要更新原始列表。

To summarize my comments below, I attempted adding this: 为了在下面总结我的评论,我尝试添加以下内容:

var bindingList = (BindingList<Branch>) cmbBranches.DataSource;
bindingList.Add(frmAddBranch.NewBranch);

But that results in the object being added to the ComboBox twice. 但这导致该对象两次添加到ComboBox。 Somehow by calling bindingList.Add it's "resetting" the data source and doubling up. 通过调用bindingList.Add某种方式“重置”数据源并加倍。 I cannot find any function that "refreshes" the data display once it's bound. 绑定数据后,我找不到任何“刷新”数据显示的功能。 Control.ResetBindings() did not work. Control.ResetBindings()无法正常工作。

Well, it doesn't work that way. 好吧,这种方式行不通。 The inner List<T> has no change notification mechanism, so adding directly to inner List<T> will not generate any change notification that would eventually reach the combo box. 内部List<T>没有更改通知机制,因此直接添加到内部List<T>不会生成任何更改通知,这些更改通知最终将到达组合框。 Most convenient way to do what you want is adding the item through the BindingList<T> instead. 执行所需操作的最便捷方法是通过BindingList<T>添加项目。

我相信您必须将这些项目直接添加到BindingList (而不是添加到支持的Branches列表中BindingList应该为您处理)。

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

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