更新数据绑定ComboBox [英] Updating a databound ComboBox

查看:130
本文介绍了更新数据绑定ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与此类似的问题:

I am having virtually the same problem as this:

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

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

However, I am trying to change the displayed strings; not add, remove, or sort. I have tried the BindingList solution provided in the referenced question, but it has not helped. I can see the combobox's DataSource property is correctly updated as I edit the items, but the contents displayed in the combobox are not those in the DataSource property.

我的代码如下:

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();

EDIT ::

RefreshItems似乎是一种私有方法。我只得到错误消息:

RefreshItems seems to be a private method. I just get the error message:

由于其保护级别,系统.Windows.Forms.ListControl.RefreshItems()'无法访问

"'System.Windows.Forms.ListControl.RefreshItems()' is inaccessible due to its protection level"

ResetBindings没有效果。

ResetBindings has no effect.

推荐答案

如果您要更改整个对象,整个SearchData对象,那么绑定列表将知道这个更改,因此正确的事件将内部被触发,组合框将更新。然而,因为你只更新一个属性,绑定列表不知道有什么改变。

If you were to change the entire object, meaning your entire SearchData object, then the bindinglist would have knowledge of this change, and therefore the correct events would internaly get fired, and the combobox would update. HOWEVER, since you're only updating one property, the bindinglist has no idea that something has changed.

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

What you need to do is have your SearchData class implement INotifyPropertyChanged. Here's a quick sample I wrote to demonstrate:

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;


    }

下面是一些测试代码: / p>

And here's some code to test:

        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,绑定列表更改,所有这些都将工作。

Since my class now implements INotifyPropertyChanged, the binding list gets "notified" when something changes, and all this will thus work.

这篇关于更新数据绑定ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆