How to add a scrollbar to a grid programmatically in WPF?
<p>I have the following grid in the xaml file:</p>

<pre><code>&lt;Grid x:Name="gridPMP" HorizontalAlignment="Left" Height="285" Margin="23,116,0,-330" Grid.Row="1" VerticalAlignment="Top" Width="1238"/&gt;
</code></pre>

<p>That grid is filled programmatically with this code .cs:</p>

<pre><code>public void loadPMPTable()
    {
        gridPMP.Children.Clear();
        gridPMP.RowDefinitions.Clear();
        gridPMP.ColumnDefinitions.Clear();

        MetodosAux aux = new MetodosAux();
        String s = problemName.Content.ToString();
        String listaPath = "./DataSaved/" + s + "/ListaDeMateriales.txt";
        String arbolPath = "./DataSaved/" + s + "/ArbolDeMateriales";
        String RIPath = "./DataSaved/" + s + "/RegistroInventarios.txt";

        int cols = NumValue + 1;
        int rows = aux.numeroLineasFichero(listaPath) + 1;

        FileStream fs = new FileStream(listaPath, FileMode.Open, FileAccess.Read);
        System.IO.StreamReader file = new System.IO.StreamReader(fs);

        String linea;

        for (int x = 0; x &lt; cols; x++)
        {
            ColumnDefinition CD = new ColumnDefinition();
            if (x==0)
            {
                CD.Width = new System.Windows.GridLength(120);
            }
            else
            {
                CD.Width = new System.Windows.GridLength(30);
            }
            //CD.Width = GridLength.Auto;
            gridPMP.ColumnDefinitions.Add(CD);
        }
        for (int y = 0; y &lt; rows; y++)
        {
            RowDefinition r = new RowDefinition();
            //r.Height = GridLength.Auto;
            r.Height= new System.Windows.GridLength(30);
            gridPMP.RowDefinitions.Add(r);
        }

        for (int x = 0; x &lt; cols; x++)
        {
            for (int y = 0; y &lt; rows; y++)
            {
                if ((y == 0) &amp;&amp; (x == 0)) //y=row index, x=column index
                {
                    TextBox t = new TextBox();
                    t.Width = 170;
                    t.IsReadOnly = true;
                    t.Text = "Elemento/Día";
                    t.FontWeight = FontWeights.UltraBold;
                    Grid.SetColumn(t, x);
                    Grid.SetRow(t, y);
                    gridPMP.Children.Add(t);

                }
                else if ((y == 0) &amp;&amp; (x &gt;= 1))
                {
                    TextBox t = new TextBox();
                    t.Width = 170;
                    t.IsReadOnly = true;
                    t.Text = x.ToString();
                    t.FontWeight = FontWeights.UltraBold;
                    Grid.SetColumn(t, x);
                    Grid.SetRow(t, y);
                    gridPMP.Children.Add(t);
                }
                else if (x == 0)
                {
                    TextBox t = new TextBox();
                    t.Width = 170;
                    t.IsReadOnly = true;
                    linea = file.ReadLine();
                    t.Text = linea;
                    t.FontWeight = FontWeights.DemiBold;
                    Grid.SetColumn(t, x);
                    Grid.SetRow(t, y);
                    gridPMP.Children.Add(t);
                }
                else
                {
                    TextBox tb = new TextBox();
                    tb.PreviewTextInput += textBoxValidator;
                    tb.Width = 170;
                    tb.Text = "0";
                    Grid.SetColumn(tb, x);
                    Grid.SetRow(tb, y);
                    gridPMP.Children.Add(tb);
                }
            }
        }
        file.Close();
        fs.Close();
    }//end loadPMPTable()
</code></pre>

<p>The number of columns and rows change depending on the structure of some files. So when there are more than 8 rows the grid doesnt display the data because there isnt enough space. That's why I want to make the grid scrollable. How can I do this programmatically or from the xaml file? I tried it from the xaml file but I didnt get it to work.</p>