当高度为自动时,如何使用ListView显示滚动条?

Ian*_*ams 2 wpf xaml windows-store-apps

我正在开发一个Windows应用商店应用.

我有一个包含大量项目的ListView.它嵌套在一些堆栈面板和网格中,但它们基本上都是自动调整大小的.从本质上讲,ListView是屏幕右半部分的大部分,无论它的大小是多少.

如果我给ListView一个硬值高度,滚动条将自动出现,没有额外的工作.大.但我不想设置一个高度...我希望它是容器中可用的所有区域.如果我试图聪明并将其设置为9999或其他东西,那么它将不会滚动.

我做了很多研究,像这样的类似问题说,持有ListView的东西不能像StackPanel那样给它无限大小.我有什么替代品?如何将ListView放在具有任意空间的内容中并显示滚动条?

我唯一的想法是,必须有一些方法,在容纳ListView的容器中,告诉ListView它具有所有可用区域,以便它的高度设置为该值.类似于如何将ListViewItem的Horizo​​ntalContentAlignment属性设置为'Stretch',以便获取ListView中的项目以了解它们实际具有的可用宽度.

我的布局的基本部分是这样的:

<!-- Nested in some other stuff simple Grids and StackPanels, none of which has hard heights set (all auto or *) -->

<!-- Even if I made this a Grid with one Row, setting definition to * or Auto doesn't help the issue - no scroll bar appears -->
<StackPanel>

    <!-- Other stuff that has Visibility="Collapsed"... I have code so that only one item at a time within this container will ever be visible, and it gets all available space. -->

    <ListView ItemsSource="{Binding SomeBigList}" ItemTemplate="{StaticResource MyDataTemplate}" />

    <!-- Other stuff that has Visibility="Collapsed" -->

</StackPanel>
Run Code Online (Sandbox Code Playgroud)

如何在不在任何地方设置硬高度的情况下让滚动条显示在ListView中?谢谢你的帮助.

Chr*_*isF 5

不要使用<StackPanel>.

如果您可以控制绘制的内容,并且任何时候只能看到一个项目,那么请使用非无限大小的容器<Grid>.您可以将每个项目放在同一行中,或者使用单独的行,只是为了便于发现它是什么.