如果高度设置为自动,WPF ListView 没有滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6221813/
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 no scrollbar if height set to auto
提问by Eatdoku
Hi i have a ListView that binds to a collection. I set the height of the ListView to auto for it to take up all the space in the region. However there is not scrollbar after i set the height to auto. If i give it a height then the scrollbar would show up.
嗨,我有一个绑定到集合的 ListView。我将 ListView 的高度设置为 auto 使其占据该区域的所有空间。但是在我将高度设置为自动后没有滚动条。如果我给它一个高度,那么滚动条就会出现。
the markup is pretty much like the following
标记非常类似于以下内容
<Grid>
<StackPanel>
<Expander>
<DataGrid>
<Expander>
<ListView>
回答by Joel B Fant
I have a hunch that your ListView
is inside a panel that allows it to expand vertically without limit.
我有一种预感,你ListView
是在一个面板内,允许它无限制地垂直扩展。
If you put a ListView
inside a StackPanel
, for example, the ListView
's height can exceed the height of the StackPanel
. The ListView
has increased its height to show all its items, as far as it's concerned, thus no scrollbar.
例如,如果将 a 放在 aListView
内StackPanel
,则 theListView
的高度可以超过StackPanel
. 在ListView
增加了它的高度,以显示其所有项目,就我们关心的,因此没有滚动条。
However, if you change that StackPanel
to a Grid
, where controls automatically try to fit themselves inside that area, the ListView
will automatically have a scrollbar when it contains more items than it can display.
但是,如果您将其更改StackPanel
为 a Grid
,其中控件会自动尝试将自己放入该区域内,则ListView
当它包含的项目多于可以显示时,将自动具有滚动条。
This will probably be solved most simply by adjusting your layout that contains the ListView
.
这可能最简单地通过调整包含ListView
.
回答by Sandeep
Instead of setting
而不是设置
<RowDefinition Height="auto"/>
set:
放:
<RowDefinition Height="1*"/>
回答by H.B.
Why should it show a scrollbar if there is nothing to scroll?
如果没有可滚动的内容,为什么要显示滚动条?
If you want to override any default behavior you can set ScrollViewer.VerticalScrollBarVisibility="Visible"
on the ListBox.
如果您想覆盖任何可以ScrollViewer.VerticalScrollBarVisibility="Visible"
在 ListBox 上设置的默认行为。