簡體   English   中英

綁定列表更新時未觸發 ListBox SelectedIndexChanged 事件

[英]ListBox SelectedIndexChanged event not fired when bound list updates

我想要一個帶有關聯添加和刪除按鈕的基本列表框。 當且僅當 ListBox 中有一個選擇時,Delete 按鈕才被啟用,並且 Add 按鈕應該將一個項目附加到 ListBox 的末尾。

我的第一次嘗試是創建一個List<string> items字段,如下所示:

public partial class Form1 : Form {
    public Form1 {
        InitializeComponent();
        this.listBox1.DataSource = this.items;
    }

    private void addButton_Click(object sender, EventArgs e) {
        this.items.Add("item {this.items.Count + 1}");
    }

    private void deleteButton_Click(object sender, EventArgs e) {
        this.items.RemoveAt(this.listBox1.SelectedIndex);
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
        this.deleteButton.enabled = (this.listBox1.SelectedIndex != -1);
    }

    List<string> items = new List<string>();
}

但是,事實證明 ListBox 似乎不會隨着列表中的項目更改而自動更新。 這是一個相關的問題

大多數建議建議只運行listBox1.DataSource = null; this.listBox1.DataSource = this.items; listBox1.DataSource = null; this.listBox1.DataSource = this.items; 每次我更新列表時,但這對我來說都違背了binding的目的。

另一個答案建議對我的列表的數據類型使用ObservableCollection ,但這並沒有什么不同。

最后我找到了BindingList類,它似乎做我想做的事; ListBox 自動反映items字段的內容。 (如果有另一種解決方案實際上是最佳實踐,而不是在 Internet 上粘貼的某些貨物崇拜民間傳說,請告訴我。)


但是,當我添加到items的第一個items出現在 ListBox 中時,該行將被選中。 我可以看到listBox1.SelectedIndex從 -1 變為 0。但是, listBox1_SelectedIndexChanged事件不會觸發 因此,刪除按鈕的狀態不會正確更新。

(順便說一句,這種 UI 模式非常普遍,在實現這么簡單的事情上有這么多相互矛盾的信息似乎是災難性的。)

我也遇到了同樣的問題。 由於這是我遇到的第一個問題,也許我的回答可以節省一些時間。

DataSource被分配一個空的BindingList並且稍后將項目添加到該BindingList ,如下所示:

private BindingList<MyType> _myList = new BindingList<MyType>();

public MyForm()
{
    listBox1.DataSource = _myList;
}

private void LoadData()
{
    foreach (MyType item in GetDataFromSomewhere())
    {
        _myList.Add(item);
    }
}

然后在添加第一項后,將在ListBox選擇,並且listBox1.SelectedIndex將為 0 - 但SelectedIndexChanged事件不會被觸發。

看起來這可能是此處此處建議的ListBox的錯誤。

一種解決方法可能是像Juan 一樣ListBox派生,或者在添加之前檢查BindingList包含任何項目:

        bool isFirstItem = (0 == _myList.Count);
        _myList.Add(item);
        if (isFirstItem)
        {
            // Your choice: set listBox1.SelectedIndex to -1 and back to 0,
            // or call the event handler yourself.
        }

嘗試手動選擇最后一個元素,而不是依賴默認行為。 這將觸發事件。

public partial class Form1 : Form
{
    BindingList<string> items = new BindingList<string>();
    public Form1()
    {
        InitializeComponent();
        this.listBox1.DataSource = items;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.deleteButton.Enabled = (this.listBox1.SelectedIndex != -1);
    }

    private void addButton_Click(object sender, EventArgs e)
    {
        this.items.Add($"item {this.items.Count + 1}");
        this.listBox1.SelectedIndex = -1;
        this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
    }

    private void deleteButton_Click(object sender, EventArgs e)
    {
        this.items.RemoveAt(this.listBox1.SelectedIndex);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM