如何在 WPF 数据网格上启用滚动条/滚动
我有一个位于 Grid
布局容器内的 DataGrid
控件,但我似乎无法在 DataGrid
上实现自动滚动自己去工作。我可以将 DataGrid
包裹在 ScrollViewer
周围,从而添加滚动条,但自动滚动不起作用。
因此,现在,当向 DataGrid
添加新条目时,DataGrid
只是垂直扩展。我希望启用垂直滚动条,以便在添加的项目数量多于原始垂直大小可显示的项目时滚动到 DataGrid 中的项目,而不是整个 DataGrid > 扩大。当然,必须有一种简单的方法来实现这一点。
I've got a DataGrid
control that's within a Grid
layout container, and I can't seem to get the auto-scroll on the DataGrid
itself to work. I can wrap the DataGrid
around a ScrollViewer
and thus adding the scroll bar, but the auto scrolling doesn't work.
So right now, when new entries are added to the DataGrid
, the DataGrid
just expands vertically. I'd like to have the vertical scroll bar enabled allowing scrolling to the items in the DataGrid
when more items are added than the original vertical size can show, instead of the whole DataGrid
expanding. Surely there's got to be an easy way to make that happen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,弄清楚了这个问题...事实证明我什至不需要将数据网格包裹在 ScrollViewer 周围。我所要做的就是定义数据网格的高度(使用“高度”属性),并且当添加超出高度的项目时会出现数据网格滚动条。显然,当高度未定义时,它是动态的,并随着新项目的添加而垂直扩展。
另一件要添加的事情是,在我的数据网格中,我还为每一行定义了行详细信息,因此当展开多个行详细信息时,将启用滚动,但滚动条行为有点古怪(就像它一样)滚动不平滑),为了使其平滑滚动,修复方法是添加以下 datagrid 属性: ScrollViewer.CanContentScroll="False" (我猜测 datagrid 控件是/继承自 ScrollViewer),然后滚动很流畅,就像正常预期的滚动行为一样。
Okay, got this one figured out ... It turns out I didn't even need to wrap the datagrid around a ScrollViewer after all. All I had to do is define the height for the datagrid (Using the "Height" attribute) and the datagrid scroll bar appears when items are added that go beyond the height. Apparently when the height isn't defined it is dynamic and expands vertically as new items are added.
Another thing to add to this was that in my datagrid, I had a row details defined for each row as well, so when multiple row details were expanded, the scrolling would be enabled, but the scroll bar behavior was a little wacky (Like it wasn't smooth scrolling), and the fix for that to make it smooth scrolling was adding the following datagrid attribute: ScrollViewer.CanContentScroll="False" (I'm guessing the datagrid control is/inherits from a ScrollViewer) and then the scrolling was smooth and like the normal expected scrolling behavior.