WPF 工具包 - 将数据网格高度调整为内容高度

发布于 2024-08-03 05:06:31 字数 149 浏览 2 评论 0原文

我有一个 WPF 数据网格,希望它的高度等于其行 + 标题的高度之和。

即,如果我的数据网格标题为 100 像素,并且有 4 行,每行高度为 50 像素,则数据网格的总高度应为 300 像素。

有没有一种方法可以声明性地指定高度应与其内容的总和相匹配?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-08-10 05:06:31

可以请您粘贴您的代码吗?因为您所要求的可以通过将 DataGrid 放入面板中来简单地实现,这不会限制其大小并将其精确地排列在控件的所需大小内。

换句话说,您可以将其以垂直方向放置在 StackPanel 中,就是这样:

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文