WPF Toolkit - Fit datagrid height to content height

女生的网名这么多〃 提交于 2019-12-13 20:08:19

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/1368140/wpf-toolkit-fit-datagrid-height-to-content-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!