如何在 WPF Datagrid 上启用滚动条/滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3444047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:00:15  来源:igfitidea点击:

How to enable scrollbar / scrolling on a WPF Datagrid

wpfdatagridscrollscrollbar

提问by BrianP

I've got a DataGridcontrol that's within a Gridlayout container, and I can't seem to get the auto-scroll on the DataGriditself to work. I can wrap the DataGridaround a ScrollViewerand thus adding the scroll bar, but the auto scrolling doesn't work.

我有一个DataGrid位于Grid布局容器内的控件,但我似乎无法使其DataGrid自身的自动滚动工作。我可以环绕DataGridaScrollViewer并因此添加滚动条,但自动滚动不起作用。

So right now, when new entries are added to the DataGrid, the DataGridjust expands vertically. I'd like to have the vertical scroll bar enabled allowing scrolling to the items in the DataGridwhen more items are added than the original vertical size can show, instead of the whole DataGridexpanding. Surely there's got to be an easy way to make that happen.

所以现在,当新条目添加到 时DataGridDataGrid只会垂直扩展。我想启用垂直滚动条,允许在DataGrid添加的项目比原始垂直大小可以显示的更多时滚动到项目,而不是整个DataGrid扩展。当然,必须有一种简单的方法来实现这一点。

回答by BrianP

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.

好的,搞清楚了这个……结果我什至不需要将数据网格包裹在 ScrollViewer 中。我所要做的就是定义数据网格的高度(使用“高度”属性),当添加超出高度的项目时,数据网格滚动条就会出现。显然,当高度未定义时,它是动态的,并且随着新项目的添加而垂直扩展。

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.

要添加的另一件事是,在我的数据网格中,我也为每一行定义了一个行详细信息,因此当展开多行详细信息时,将启用滚动,但滚动条的行为有点古怪(喜欢它不是平滑滚动),为了使它平滑滚动,修复方法是添加以下数据网格属性:ScrollViewer.CanContentScroll="False"(我猜数据网格控件是/继承自 ScrollViewer)然后滚动很流畅,就像正常的预期滚动行为。