WPF DataGrid 垂直调整大小
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到此行为是因为
HeaderedContentControl
的默认模板使用StackPanel
来显示其内容。由于StackPanel
采用其子项的大小,因此DataGrid
会扩展其高度,使其每个项目都显示在屏幕上,而无需滚动条。然后,由于HeaderedContentControl
的大小,显示内容会被裁剪。更改模板以使用
Grid
或DockPanel
可以解决此问题:You're seeing this behavior because the default template for
HeaderedContentControl
is using aStackPanel
to show its contents. Since theStackPanel
takes the size of its children, theDataGrid
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 theHeaderedContentControl
.Changing the template to use a
Grid
or aDockPanel
solves this problem: