我有一个UserControl(如下所示的XAML),其中包含一个ListBox,我想在WrapPanel中显示图像,其中图像尽可能多地显示在一行上,然后换到下一行等等。它可以正常工作,但当ListBox高度超过窗口可用空间时,我没有垂直滚动条,即内容被剪切了。如果我在ListBox上设置固定高度,则滚动条会出现并按预期工作。如何让这个listbox增长到可用空间,并显示垂直滚动条?此控件位于主窗口中的Grid内的StackPanel内。如果我将StackPanel包装在ScrollViewer中,则会获得所需的滚动条,但是如果我要在ListBox上方添加一些控件(例如图像大小“缩放”等),那么这并不是一个好的解决方案,因为我不希望它们与图像一起滚动。
谢谢! :)
<UserControl x:Class="GalleryAdmin.UI.GalleryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="LightGray" Margin="5" >
<StackPanel Margin="5">
<Image Source="{Binding Path=LocalThumbPath}" Height="100" />
<TextBlock Text="{Binding Path=Name}" TextAlignment="Center"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>