Q:

I want to use a Button to add the text in a TextBox to a ListView

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new AddItem();

        taskList.ItemsSource = new List<AddItem>
        {
            new AddItem()
            {
                Title = "Task1",
            },
            new AddItem()
            {
                Title = "Task2",
            },
        };
    }
   
    private void addButton_Click(object sender, RoutedEventArgs e)
    {
        taskList.SelectedItem = new AddItem();
    }

I want to add the Text in the TextBox as an AddItem to the ListView using a Button, but I don't know how to add the value to the ListView. The name of the TextBox is inputTitle.

A:

If you intend to modify the collection you have to use an ObservableCollection that supports notifying collection changes through the INotifyCollectionChanged interface that tiggers updating your ListView. List<T> does not support that, so your user interface will not be updated when adding, removing or replacing items in the collection.
public partial class MainWindow : Window
{
   private ObservableCollection<AddItem> _myItems;

   public MainWindow()
   {
      InitializeComponent();

      DataContext = new AddItem();

      _myItems = new ObservableCollection<AddItem>
      {
         new AddItem()
         {
            Title = "Task1",
         },
         new AddItem()
         {
            Title = "Task2",
         },
      };

      taskList.ItemsSource = _myItems;
   }

   private void addButton_Click(object sender, RoutedEventArgs e)
   {
      var newItem = new AddItem()
      {
         Title = inputTitle.Text,
      };

      _myItems.Add(newItem);
      taskList.SelectedItem = newItem;
   }
}

You should consider checking if the Text is empty, so you avoid adding useless items or deactivate the button if the TextBox is empty.

