更新数据绑定组合框组合、绑定、数据

由网友(在最好的年纪遇到最好的你)分享简介:我有几乎相同的问题,因为这样的:I am having virtually the same problem as this: http://stackoverflow.com/questions/433281/c-update-combobox-bound-to-generic-list不过,我试图改变所显示的字...

我有几乎相同的问题,因为这样的:

I am having virtually the same problem as this:

http://stackoverflow.com/questions/433281/c-update-combobox-bound-to-generic-list

不过,我试图改变所显示的字符串;没有添加,删除或排序。我曾在引用的问题所提供的的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.

我的code如下所示:

my code looks as follows:

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";

...

当我尝试更新我做了以下内容:

When I try to update the content I do the following:

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

:

RefreshItems似乎是一个私有方法。我刚刚得到的错误信息:

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

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

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

ResetBindings没有效果。

ResetBindings has no effect.

推荐答案

如果你要改变整个对象,这意味着你的整个SearchData对象,那么的BindingList会有这种变化的认识,因此正确的事件将internaly得到解雇,组合框会更新。但是,因为你只更新一个属性时,的BindingList不知道的东西发生了变化。

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;


    }

和这里的一些code测试:

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.

阅读全文

相关推荐

最新文章