【问题标题】:Updating a databound ComboBox更新数据绑定 ComboBox
【发布时间】:2010-10-05 00:13:38
【问题描述】:

我遇到了与此几乎相同的问题:

C# Update combobox bound to generic list

但是,我正在尝试更改显示的字符串;不添加、删除或排序。我已经尝试过引用问题中提供的 BindingList 解决方案,但没有帮助。 我可以看到组合框的 DataSource 属性在我编辑项目时正确更新,但组合框中显示的内容不是 DataSource 属性中的内容。

我的代码如下:

mSearchComboData = new List<SearchData>();
mSearchComboData.Add(new SearchData("", StringTable.PatientID));
mSearchComboData.Add(new SearchData("", StringTable.LastName));
mSearchComboData.Add(new SearchData("", StringTable.LastPhysician));
mSearchComboData.Add(new SearchData("", StringTable.LastExamDate));

mBindingList = new BindingList<SearchData>(mSearchComboData);

SearchComboBox.Items.Clear();
SearchComboBox.DataSource = mBindingList;
SearchComboBox.ValueMember = "Value";
SearchComboBox.DisplayMember = "Display";

...

当我尝试更新内容时,我会执行以下操作:

int idx = SearchComboBox.SelectedIndex;
mBindingList[idx].Display = value;
SearchComboBox.Refresh();

编辑::

RefreshItems 似乎是一个私有方法。我刚刚收到错误消息:

“'System.Windows.Forms.ListControl.RefreshItems()' 由于其保护级别而无法访问”

ResetBindings 无效。

【问题讨论】:

    标签: c# .net combobox refresh


    【解决方案1】:

    如果您要更改整个对象,即您的整个 SearchData 对象,那么绑定列表将知道此更改,因此正确的事件将在内部被触发,并且组合框将更新。但是,由于您只更新一个属性,绑定列表不知道发生了什么变化。

    您需要做的是让您的 SearchData 类实现 INotifyPropertyChanged。这是我写的一个快速示例来演示:

    public class Dude : INotifyPropertyChanged
        {
            private string name;
            private int age;
    
            public int Age
            {
                get { return this.Age; }
                set
                {
                    this.age = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                    }
                }
            }
            public string Name
            {
                get
                {
                    return this.name;
                }
    
                set
                {
                    this.name = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
    
        }
    

    这里有一些代码要测试:

            private void button1_Click(object sender, EventArgs e)
            {
                //Populate the list and binding list with some random data  
                List<Dude> dudes = new List<Dude>();
                dudes.Add(new Dude { Name = "Alex", Age = 27 });
                dudes.Add(new Dude { Name = "Mike", Age = 37 });
                dudes.Add(new Dude { Name = "Bob", Age = 21 });
                dudes.Add(new Dude { Name = "Joe", Age = 22 });
    
                this.bindingList = new BindingList<Dude>(dudes);
                this.comboBox1.DataSource = bindingList;
                this.comboBox1.DisplayMember = "Name";
                this.comboBox1.ValueMember = "Age";
    
            }
    
    
        private void button3_Click(object sender, EventArgs e)
        {
            //change selected index to some random garbage
            this.bindingList[this.comboBox1.SelectedIndex].Name = "Whatever";
        }
    

    由于我的类现在实现了 INotifyPropertyChanged,因此绑定列表会在发生更改时得到“通知”,因此所有这些都会起作用。

    【讨论】:

    • 我尝试了大约 13 种不同的方法来更新表单上的组合框,然后终于找到了这个解决方案。写得很好,工作就像一个魅力。谢谢!
    【解决方案2】:

    而不是SearchComboBox.Refresh();

    试试SearchComboBox.RefreshItems();

    SearchComboBox.ResetBindings();

    我认为你需要的确实是后者。

    您可以访问其成员 here 的文档。

    【讨论】:

    • RefreshItems 受保护
    【解决方案3】:

    这是一篇旧帖子,但可能有用。

    我刚刚查看了同样的问题,发现如果您在 BindingList 对象上调用 ResetItem,并且更改了 Items 位置,它会在内部为您引发 Itemchanged 通知事件,从而导致列表更新。

    int idx = SearchComboBox.SelectedIndex;
    mBindingList[idx].Display = value;
    
    mBindingList.ResetItem(idx); //raise Item changed event to update the list display
    

    【讨论】:

      猜你喜欢
      • 2012-01-03
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2012-06-18
      • 2011-05-06
      • 2019-06-23
      相关资源
      最近更新 更多