简体   繁体   English

基础ObservableCollection更改时,ComboBox项未更新

[英]ComboBox items not updating when underlying ObservableCollection changes

I have a ComboBox and an ObservableCollection set as DataSource for that ComboBox . 我有一个ComboBox和一个ObservableCollection设置为该ComboBox DataSource
When I programmatically add/remove items from the observable collection, nothing changes in the ComboBox . 当我以编程方式从可观察的集合中添加/删除项目时, ComboBox没有任何变化。
What am I doing wrong? 我究竟做错了什么?
Part 2: tried to put a BindingSource as a proxy for ObservableCollection . 第2部分:尝试将BindingSource用作ObservableCollection的代理。 When programmatically added/removed items from ObservableCollection , no event like ListChanged or similar fired. ObservableCollection编程方式添加/删除项目时,不会触发类似ListChanged或类似事件的事件。
How can I make a ComboBox automatically update its list when underlying collection changes? 底层集合更改时,如何使ComboBox自动更新其列表?

    public Form1()
    {
        InitializeComponent();

        comboBox1.DataSource = new ObservableCollection<MyItem>(
            new []
            {
                new MyItem() { Name = "AAA"},
                new MyItem() { Name = "BBB"},
            });
    }

    private void Button3_Click(object sender, EventArgs e)
    {
        // Nothing changes in the ComboBox when I add a new item to ObservableCollection
        ((ObservableCollection<MyItem>)(comboBox1.DataSource))
            .Add(new MyItem() { Name = Guid.NewGuid().ToString()});
    }
}

public class MyItem
{
    public string Name { get; set; }
}

It helps to wrap a list in a BindingList<T> . 它有助于将列表包装在BindingList<T> Here a little test code: 这里是一些测试代码:

public partial class Form1 : Form
{
    private readonly List<string> _coll = new List<string> { "aaaaa", "bbbbb", "ccccc" };
    private readonly BindingList<string> _blist;
    private readonly Random _rand = new Random();
    private const string Templ = "mcvnoqei4yutladfffvtymoiaro875b247ytmlarkfhsdmptiuo58y1toye";

    public Form1()
    {
        InitializeComponent();
        _blist = new BindingList<string>(_coll);
        comboBox1.DataSource = _blist;
    }

    private void AddButton_Click(object sender, EventArgs e)
    {
        int i = _rand.Next(Templ.Length - 5);
        string s = Templ.Substring(i, 5);
        _blist.Add(s);
    }
}

Note that you have to make the changes (Add, Remove etc.) to the BindingList . 请注意,您必须对BindingList进行更改(添加,删除等)。 The BindingSource works the same way. BindingSource以相同的方式工作。

暂无
暂无

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

相关问题 具有复合集合的组合框不会随着对observablecollection的更改而更新 - Combobox with composite collection not updating with changes to observablecollection 当基础ComboBox项目描述更改时,绑定不会在ComboBox文本字段中更新 - Binding not updating in ComboBox Text field when underlying ComboBox item description changes 在另一个View中修改ObservableCollection时,组合框未更新 - ComboBox not updating when ObservableCollection is modified in another View 将项目添加到基础ObservableCollection时,递归TreeView不会保留结构 - Recursive TreeView does not retain structure when items added to underlying ObservableCollection 的ObservableCollection <DateTime> 并在项目更改时更新用户界面 - ObservableCollection<DateTime> and updating the UI when the item changes 在ObservableCollection中更改模型属性时更新UI? - Updating UI when a model property changes in an ObservableCollection? 绑定的ObservableCollection更改时,ListView不更新 - ListView not updating when the bound ObservableCollection changes Observablecollection在更新属性时更新多个项目 - Observablecollection updates multiple items when updating a property 将项目添加到Silverlight中的ObservableCollection时,DataGrid不更新 - DataGrid not updating when items added to ObservableCollection in Silverlight 基础数据源更改时,UI中的DataGridView不更新 - DataGridView not updating in UI when underlying datasource changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM