簡體   English   中英

為什么在添加新項目時我的列表框沒有更新?

[英]Why isnt my listbox getting updated with new items when i add them?

我試圖每次按添加按鈕時將新項目添加到列表框中,但是由於某種原因,它僅添加第一個項目,如果再次按下它,則不添加第二個項目。

我現在看到的代碼是創建一個名為_items的新列表,然后每次按下按鈕時都在文本框中添加內容,然后還要更新ItemSource。

每次按下AddBtn時如何添加新項?

List<string> _items = new List<string>();


private void addBtn_Click(object sender, RoutedEventArgs e)
{
    _items.Add(recipentTextbox.Text);
    recipientLb.ItemsSource = _items;
}

嘗試使用ObservableCollection<string>而不是List<string> ObservableCollection支持數據綁定,並將更新target屬性。

ObservableCollection<string> _items = new ObservableCollection<string>();

//  Or whatever your constructor is
public MainWindow()
{
    recipientLb.ItemsSource = _items;
}

private void addBtn_Click(object sender, RoutedEventArgs e)
{
    _items.Add(recipentTextbox.Text);
}

暫無
暫無

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

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