title: WPF Datagrid Row Height To Match Datagrid Height Without Scroll
body: <p>I have a user control that have a Datagrid with customized DataTemplate as below :-</p>
  
  <pre><code>&lt;UserControl x:Class="POCSurveySystem.UI.Windows.QuestionListing"
           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" &gt;
  &lt;UserControl.Resources&gt;
      &lt;Style x:Key="RadioButtonItemStyle" TargetType="{x:Type ListBoxItem}"&gt;
          &lt;Setter Property="Margin" Value="0,0,5,0" /&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt;
                      &lt;Border BorderThickness="0" Background="Transparent"&gt;
                          &lt;!-- Note: IsChecked is bound to IsSelected--&gt;
                          &lt;RadioButton 
                      Focusable="False" 
                      IsHitTestVisible="False" 
                      IsChecked="{TemplateBinding IsSelected}"&gt;
                              &lt;ContentPresenter /&gt;
                          &lt;/RadioButton&gt;
                      &lt;/Border&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
      &lt;ItemsPanelTemplate x:Key="HorizontalItemsPanel"&gt;
          &lt;VirtualizingStackPanel 
      Orientation="Horizontal" /&gt;
      &lt;/ItemsPanelTemplate&gt;
  &lt;/UserControl.Resources&gt;
  &lt;Grid Background="AliceBlue"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="30" /&gt;
          &lt;RowDefinition Height="30"/&gt;
          &lt;RowDefinition Height="*"/&gt;
          &lt;RowDefinition Height="23" /&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;StackPanel Orientation="Horizontal" Grid.Row="1"&gt;
          &lt;Label Name="GroupQuestionHeader" FontSize="14" FontWeight="Bold" FontFamily="Times New Roman" HorizontalAlignment="Left" /&gt;
          &lt;Label Name="PageCount" FontSize="10" FontFamily="Times New Roman" HorizontalAlignment="Right"&gt;&lt;/Label&gt;
      &lt;/StackPanel&gt;
  
  
      &lt;DockPanel Grid.Row="2" VerticalAlignment="Stretch"&gt;
          &lt;DataGrid VerticalScrollBarVisibility="Disabled" VerticalAlignment="Stretch" AutoGenerateColumns="False" HorizontalAlignment="Left" Name="dataGridQuestion" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserAddRows="False" GridLinesVisibility="All" HorizontalGridLinesBrush="#FFDEDEDE" Height="400" MaxHeight="400"&gt;
              &lt;DataGrid.CellStyle&gt;
                  &lt;Style TargetType="DataGridCell"&gt;
                      &lt;!--&lt;Setter Property="Padding" Value="5" /&gt;--&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="{x:Type DataGridCell}"&gt;
                                  &lt;Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"&gt;
                                      &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
                                  &lt;/Border&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                      &lt;Style.Triggers&gt;
                          &lt;Trigger Property="IsSelected" Value="true"&gt;
                              &lt;Setter Property="Background" Value="Transparent" /&gt;
                              &lt;Setter Property="Foreground" Value="Black" /&gt;
                              &lt;Setter Property="BorderBrush" Value="{x:Null}" /&gt;
                          &lt;/Trigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/DataGrid.CellStyle&gt;
  
              &lt;DataGrid.Columns&gt;
  
                  &lt;DataGridTemplateColumn Header="Question" Width="2*"&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBlock TextWrapping="Wrap" Text="{Binding QuestionContent, Mode=OneWay}" /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
                  &lt;DataGridTemplateColumn Header="We fully Comply | We partly Comply | We do not Comply" Width="1*"&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;!--&lt;ListBox 
                              BorderThickness="0" 
                              SelectedValue="{Binding MyDataListSelectedValue}" 
                              ItemContainerStyle="{StaticResource RadioButtonItemStyle}" 
                              ItemsPanel="{StaticResource HorizontalItemsPanel}" Name="OptionsRadioButtonGroup" HorizontalContentAlignment="Left"
                                  Cursor="Hand" HorizontalAlignment="Left"&gt;
                                  &lt;ListBoxItem Width="90" Name="AGR"/&gt;
                                  &lt;ListBoxItem Width="90" Name="PGR"/&gt;
                                  &lt;ListBoxItem Name="DNR"/&gt;
                              &lt;/ListBox&gt;--&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/DockPanel&gt;
      &lt;StackPanel Orientation="Horizontal" Grid.Row="3"&gt;
          &lt;Button Content="Next Page" Height="23" HorizontalAlignment="Left" Name="btnNext" VerticalAlignment="Top" Width="75" Click="btnNext_Click"  Margin="5,0,0,0" /&gt;
          &lt;Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="86,0,0,0" Name="btnSubmit" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="btnSubmit_Click" /&gt;
      &lt;/StackPanel&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>However, the datagridview does not shrink / stretch as per the rows of data grow. I have tried to hardcode dataGridQuestion.MinRowHeight = 100 however that is not what I looking for as the Textblock in the datagrid column may varied. </p>
  
  <p>Question :
  How to avoid the grey area which was shown in the pic as below after the last row of the datagrid row? I tested using dataGridQuestion.MinRowHeight = dataGridQuestion.Height / DataEntityList.Count but it still there..</p>
  
  <p>How to make Datagrid to shrink and expand as rows of data bound decrease / increase? </p>
  
  <p><img src="https://i.stack.imgur.com/0ZAQ7.png" alt="Grey Area Below the last row of datagrid row"></p>