title: Dynamically Adding TextBox using a Button within MVVM framework
body: <p>I keep climbing the steep WPF hill! So I want to create a UI that allows the user to dynamically add a text box.  To do this they would hit a button. </p>
  
  <p>I've managed to create this using code behind but I want to move towards an MVVM structure so I don't have any code in the view. I've tried ICommand and ObservableCollection but I'm missing something and I don't know where.  Here is my simple example.</p>
  
  <p>XAML: Very basic with one button that adds a row.</p>
  
  <pre><code>&lt;Window x:Class="WPFpractice072514.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:WPFpractice072514"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid Name="mymy" &gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;ColumnDefinition Width="Auto" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Button Grid.Column="0" Grid.Row="0" Name="ButtonUpdateArtist"
                  Content="Add TextBox" Click="ButtonAddTexboxBlockExecute" /&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>C# Code Behind</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WPFpractice072514
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          #region members
          int count = 0;
          #endregion
  
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void ButtonAddTexboxBlockExecute(Object Sender, RoutedEventArgs e)
          {
              TextBox t = new TextBox();
              t.Height = 20;
              t.Width = 20;
              t.Name = "button";
  
              RowDefinition rowDef1;
              rowDef1 = new RowDefinition();
              mymy.RowDefinitions.Add(rowDef1);
  
              ColumnDefinition colDef1;
              colDef1 = new ColumnDefinition();
              mymy.ColumnDefinitions.Add(colDef1);
              ++count;
  
              mymy.Children.Add(t);
  
              Grid.SetColumn(t, 1);
              Grid.SetRow(t, count);
  
          }
      }
  }
  </code></pre>
  
  <p>Questions: What code (XAML and C#) do I need to be able to move the method out of the code behind and into a viewmodel?</p>
  
  <p>Can you use commands to dynamically add a textbox?</p>
  
  <p>I'm assuming that the textboxes must be kept in a container which in this case is what grid is for. But if I'm using an MVVM do I need to contain the textboxes in a listview or some other container that uses ItemsSource?</p>