title: C# WPF - Make DataGrid stretch to available size, not expand the available size
body: <p>I use Visual Studio 2010.  I've been struggling trying to get a datagrid to fill the existing space.  If necessary, to show a scrollbar on the datagrid, not on the page.</p>
  
  <p>Example code:</p>
  
  <pre><code>&lt;Page x:Class="TestApp.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300"
  Title="Page1"&gt;
  &lt;Grid&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="20"/&gt;
          &lt;ColumnDefinition Width="*"/&gt;
          &lt;ColumnDefinition Width="20"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="20"/&gt;
          &lt;RowDefinition Height="200"/&gt;
          &lt;RowDefinition Height="20"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Expander x:Name="MyExpander"
                Grid.Column="1"
                Grid.Row="1"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Top"
                Header="My Expander"
                Expanded="MyExpander_Expanded"
                Collapsed="MyExpander_Collapsed"&gt;
          &lt;Viewbox Stretch="Uniform"&gt;
          &lt;StackPanel&gt;
              &lt;DataGrid Margin="5"
                        Height="100"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        HorizontalScrollBarVisibility="Auto"
                        VerticalScrollBarVisibility="Auto"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="MyFirstColumn"/&gt;
                  &lt;DataGridTextColumn Header="MySecondColumn"/&gt;
                  &lt;DataGridTextColumn Header="MyThirdColumn"/&gt;
                  &lt;DataGridTextColumn Header="MyFourthColumn"/&gt;
                  &lt;DataGridTextColumn Header="MyFifthColumn"/&gt;
                  &lt;DataGridTextColumn Header="MySixthColumn"/&gt;
                  &lt;DataGridTextColumn Header="MySeventhColumn"/&gt;
                  &lt;DataGridTextColumn Header="MyEighthColumn"/&gt;
                  &lt;DataGridTextColumn Header="MyNinthColumn"/&gt;
                  &lt;DataGridTextColumn Header="MyTenthColumn"/&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
          &lt;Button x:Name="DoSomething"
                  Margin="5"
                  Width="100"
                  Height="30"
                  Content="Do Something"
                  Click="DoSomething_Click"/&gt;
          &lt;/StackPanel&gt;
          &lt;/Viewbox&gt;
      &lt;/Expander&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>The problem is, it seems, whatever I do, the datagrid will always stretch until all columns are completely visible, even if it stretches way past the window (or page) size.
  Note: I have a panel on the right, showing status, dates, info, ..., which should stay on the screen.</p>
  
  <p>I have tried too many things to list them all.  The only working 'solution' I found is adding another control (invisible) like a separator on the grid and binding the expander width to the width of the separator.  It works, but it's crappy programming.</p>
  
  <pre><code>MaxWidth="{Binding ElementName=MySeparator, Path=ActualWidth}"
  </code></pre>
  
  <p>Binding the width of the stackpanel to the (actual) width of the column just make the stackpanel disappear.</p>
  
  <p>Can someone please help me.  Thank you.</p>
  
  <p>-- Update --
  This is what I like to achieve:
  <a href="https://i.stack.imgur.com/QPLN3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QPLN3.png" alt="enter image description here"></a></p>
  
  <p>It seems that if I use hardcoded column widths, this problem doesn't occur.  Only if there is a column that has a star-length, the datagrid takes as much space as it needs.</p>