Search code examples
.netwpfwpf-controlswpftoolkit

WPF Toolkit - Fit datagrid height to content height


I have a WPF datagrid and would like it's height to equal the sum of the heights of it's rows + header.

i.e. if my datagrid header is 100px and I have 4 rows, each 50px in height the total height of my datagrid should be 300px.

Is there a way of declaratively specifying that the height should match the sum of it's contents?


Solution

  • Could you please paste your code? Because what you are asking can be simply achieved by placing DataGrid into a panel, which will not constrain its size and arrange it exactly within desired size of a control.

    In other words, you can place it inside StackPanel with vertical orientation, and that's it:

    <Window x:Class="WpfApplication5.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
            Title="Grid Sample"
            Height="300"
            Width="300">
      <StackPanel>
        <dg:DataGrid ItemsSource="{Binding}">
          <dg:DataGrid.Columns>
            <dg:DataGridTextColumn Header="No."
                                   Width="SizeToCells"
                                   Binding="{Binding CheckNumber}"
                                   IsReadOnly="True" />
            <dg:DataGridTextColumn Header="Date"
                                   Binding="{Binding Date, StringFormat=d}" />
            <dg:DataGridTextColumn Header="Pay To"
                                   MinWidth="200"
                                   Binding="{Binding Recipient}"
                                   CanUserSort="False" />
          </dg:DataGrid.Columns>
        </dg:DataGrid>
      </StackPanel>
    </Window>