繁体   English   中英

更新数据绑定组合框

[英]Updating a databound ComboBox

我有几乎同样的问题:

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

...

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

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

编辑::

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

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

ResetBindings无效。

如果你要更改整个对象,意味着整个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,因此当某些内容发生更改时,绑定列表会“通知”,因此所有这些都将起作用。

而不是SearchComboBox.Refresh();

尝试SearchComboBox.RefreshItems();

SearchComboBox.ResetBindings();

我认为你真的需要后者。

您可以在此处访问其成员的文档。

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

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

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

mBindingList.ResetItem(idx); //raise Item changed event to update the list display

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM