更新数据绑定组合框

Dan*_*gel 7 .net c# combobox refresh

我有几乎同样的问题:

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

但是,我正在尝试更改显示的字符串; 不添加,删除或排序.我已经尝试了引用问题中提供的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";

...
Run Code Online (Sandbox Code Playgroud)

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

int idx = SearchComboBox.SelectedIndex;
mBindingList[idx].Display = value;
SearchComboBox.Refresh();
Run Code Online (Sandbox Code Playgroud)

编辑::

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

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

ResetBindings无效.

BFr*_*ree 11

如果你要更改整个对象,意味着整个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;


    }
Run Code Online (Sandbox Code Playgroud)

这里有一些代码要测试:

        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";
    }
Run Code Online (Sandbox Code Playgroud)

由于我的类现在实现了INotifyPropertyChanged,因此当某些内容发生更改时,绑定列表会"通知",因此所有这些都将起作用.