簡體   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