WPF DataGrid 垂直调整大小

发布于 2024-09-06 11:08:38 字数 1197 浏览 4 评论 0原文

我想将 DataGrid 放置在 HeaderedContentControl 内,但 DataGrid 没有垂直滚动条。它的大小似乎可以同时容纳所有行,底部从视图中消失。

如果我将相同的 DataGrid 放入边框元素中,我确实会得到我想要的行为。

我已将其简化为这个最小的示例:

<Grid>
    <HeaderedContentControl  Margin="10,10,10,161" >
        <HeaderedContentControl.Header >test</HeaderedContentControl.Header>

        <!-- I want it Here but then no Vertical Scroll-->
        <DataGrid ItemsSource="{Binding Path=AllData}"                      
                  AutoGenerateColumns="True"  />
    </HeaderedContentControl>

    <Border Margin="10,169,10,10">                                
        <!--Here it does scroll -->
        <DataGrid ItemsSource="{Binding Path=AllData}" 
                  AutoGenerateColumns="True"  />
    </Border>                      
</Grid>

一些注意事项:

  • 我无法使用 HeaderedContentControl.VerticalContentAlignment 使其工作,
  • 此问题与 这个问题,但我想我已经扩大了它的范围,并且有一个更好的答案。
  • 在 DataGrid 周围使用 ScrollViewer 不是一个解决方案,因为它会将标题滚动到看不见的地方。
  • 我正在使用 WPF4

I want to place a DataGrid inside a HeaderedContentControl but the the DataGrid does not get a vertical Scrollbar. It appears to be sized to hold all rows at once, the bottom disappearing from view.

If I place the same DataGrid in a Border elelemnt I do get the behaviour I want.

I have reduced it to this minimal example:

<Grid>
    <HeaderedContentControl  Margin="10,10,10,161" >
        <HeaderedContentControl.Header >test</HeaderedContentControl.Header>

        <!-- I want it Here but then no Vertical Scroll-->
        <DataGrid ItemsSource="{Binding Path=AllData}"                      
                  AutoGenerateColumns="True"  />
    </HeaderedContentControl>

    <Border Margin="10,169,10,10">                                
        <!--Here it does scroll -->
        <DataGrid ItemsSource="{Binding Path=AllData}" 
                  AutoGenerateColumns="True"  />
    </Border>                      
</Grid>

A few notes:

  • I couldn't get it to work using HeaderedContentControl.VerticalContentAlignment
  • this problem is related to this question but I think I've broadened it a bit and that there is a better answer.
  • using a ScrollViewer around the DataGrid is not a solution because it scrolls the header out of sight.
  • I am using WPF4

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

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

发布评论

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

评论(1

空名 2024-09-13 11:08:38

您看到此行为是因为 HeaderedContentControl 的默认模板使用 StackPanel 来显示其内容。由于StackPanel 采用其子项的大小,因此DataGrid 会扩展其高度,使其每个项目都显示在屏幕上,而无需滚动条。然后,由于 HeaderedContentControl 的大小,显示内容会被裁剪。

更改模板以使用 GridDockPanel 可以解决此问题:

<Style TargetType="{x:Type HeaderedContentControl}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
        <DockPanel>
          <ContentPresenter DockPanel.Dock="Top" ContentSource="Header" />
          <ContentPresenter />
        </DockPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

You're seeing this behavior because the default template for HeaderedContentControl is using a StackPanel to show its contents. Since the StackPanel takes the size of its children, the DataGrid expands its height to have every of its items shown on the screen without scrollbars. The display is then cropped due to the size of the HeaderedContentControl.

Changing the template to use a Grid or a DockPanel solves this problem:

<Style TargetType="{x:Type HeaderedContentControl}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
        <DockPanel>
          <ContentPresenter DockPanel.Dock="Top" ContentSource="Header" />
          <ContentPresenter />
        </DockPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文