WPF: How do I create a Grid dynamically that scales?
<p>I am working on a demo app, and I'm trying to set the grid size base on a selection from a combo box. The problem I am having is when a do a 2x2 grid, for example. It doesn't fill the grid; just a quarter of it. I've tried messing with the size of the rows and columns and also the control that goes inside but I can't get it to behave the way I want it to. The behavior I'm looking for is too evenly divide up space for the rows and columns; and then have a control fill the cell. When I resize the window I want the grid and the controls in each cell too resize with the window.</p>

<p>Here is my code:</p>

<pre><code>int row = 0;
int col = 0;
short cam = 0;
for (int i = 0; i &lt; (_length * _length); i++)
{
    RowDefinition rowDef = new RowDefinition();
    ColumnDefinition colDef = new ColumnDefinition();

    //rowDef.Height = GridLength.Auto;
    //colDef.Width = GridLength.Auto;
    //rowDef.Height = new GridLength((this.ActualHeight / length) - 7, GridUnitType.Star);
    //colDef.Width = new GridLength((this.ActualWidth / length) - 1, GridUnitType.Star);
    //var converter = new GridLengthConverter();
    //rowDef.Height = (GridLength)converter.ConvertFromString("*");
    //colDef.Width = (GridLength)converter.ConvertFromString("*");
    Border border = new Border();
    border.BorderBrush = Brushes.White;
    border.BorderThickness = new Thickness(2);

    //rowDef.MinHeight = (this.ActualHeight / _length) - 7;
    //colDef.MinWidth = (this.ActualWidth / _length) - 1;

    rendererGrid.RowDefinitions.Add(rowDef);
    rendererGrid.ColumnDefinitions.Add(colDef);

    AxCVClientControlLib.AxCVVideo axCVVideo = new AxCVClientControlLib.AxCVVideo();
    _hosts.Add(new System.Windows.Forms.Integration.WindowsFormsHost());
    _hosts[i].Child = axCVVideo;
    _hosts[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    _hosts[i].VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
    border.Child = _hosts[i];
    //hosts[i].Width = (this.ActualWidth / length) - 1;
    //hosts[i].Height = (this.ActualHeight / length) - 7;

    axCVVideo.CreateControl();
    axCVVideo.Camera = Convert.ToInt16(camCombo.SelectedIndex);
    axCVVideo.XResolution = 640;
    axCVVideo.YResolution = 480;
    //System.Drawing.Rectangle rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
    //axCVVideo.XResolution = (int)rect.Height;
    //axCVVideo.YResolution = (int)rect.Width;

    if (axCVVideo.ConnectSync())
    {
        axCVVideo.Connect();
    }
    _axCVs.Add(axCVVideo);


    if (col == _length)
    {
        col = 0;
        row++;
    }

    Grid.SetRow(border, row);
    Grid.SetColumn(border, col);
    rendererGrid.Children.Add(border);

    //Grid.SetRow(hosts[i], row);
    //Grid.SetColumn(hosts[i], col);
    //rendererGrid.Children.Add(hosts[i]);

    col++;
</code></pre>