如果列表中没有项目,WPF ListView 不显示水平滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16737134/
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
WPF ListView does not display horizontal scroll bars if there are no items in the list
提问by Justin
I have the following straightforward list view in a window that is 300px by 300px in size.
我在大小为 300 像素 x 300 像素的窗口中有以下简单的列表视图。
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Width="400" Header="test" />
</GridView>
</ListView.View>
<!-- <ListViewItem /> -->
</ListView>
When the ListViewItemis present a horizontal scroll bar appears as expected, however as soon as the list view is empty horizontal scroll bars will not appear even if I resize the columns to be wider than the window.
当ListViewItem出现时,水平滚动条会按预期出现,但是一旦列表视图为空,即使我将列的大小调整为比窗口宽,水平滚动条也不会出现。


Is there an easy way to modify the list view so that when the columns are too wide the horizontal scroll bars appear as in the first screenshot, even when there are no items in the list view? (I don't want the scroll bar to be permenantly visible, I just don't want the visibility to depend on whether or not there are items in the list view).
有没有一种简单的方法来修改列表视图,以便当列太宽时,水平滚动条会像第一个屏幕截图一样出现,即使列表视图中没有项目?(我不希望滚动条永久可见,我只是不希望可见性取决于列表视图中是否有项目)。
回答by Sean Cogan
If you wrap the ListViewin a ScrollViewer, I think the scrollbar will appear when the ListView is present, but the bar itself will only appear when there are items in the ListView. You can also configure the ScrollViewerto appear under different conditions, such as
如果将 包装ListView在ScrollViewer 中,我认为当 ListView 存在时滚动条会出现,但只有当ListView. 您还可以配置ScrollViewer在不同条件下出现,例如
<ScrollViewer HorizontalScrollBarVisibility="Auto">

