Reputation: 321
I'm trying to add a new item to the listbox everytime I press the add button but for some reason it only adds the first one, and if I press it again, it doesnt add the second one.
How I am seeing the code right now is that I create a new list named _items and then I add what ever is in my textbox everytime I press the button then I update the ItemSource aswell.
How do I make it add a new item everytime I press the AddBtn?
List<string> _items = new List<string>();
private void addBtn_Click(object sender, RoutedEventArgs e)
{
_items.Add(recipentTextbox.Text);
recipientLb.ItemsSource = _items;
}
Upvotes: 0
Views: 46
Reputation: 684
Try using an ObservableCollection<string>
instead of List<string>
. The ObservableCollection
supports data binding, and will update the target property.
Upvotes: 2
Reputation: 37059
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);
}
Upvotes: 0