我正在尝试在WPF/XAML中创建一个控件,用于显示水平图像列表。列表框的宽度是固定的(无滚动条)。当添加新项目时,现有项目会减少所显示的图像数量以容纳它(实际图像大小不变,只是显示的图像数量减少)。该功能类似于向具有相对宽度属性("*")的网格添加新列,并且该列包含固定宽度的图像。以下是我的代码:
<Window.Resources>
<ItemsPanelTemplate x:Key="ListBox_HorizontalItems">
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
<DataTemplate x:Key="ListBox_DataTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Image Width="150" Source="{Binding ImageSource}" />
</Grid>
</DataTemplate>
<Style x:Key="ListBox_Style_Horizontal" TargetType="ListBox">
<Setter Property="Width" Value="150" />-->
<Setter Property="ItemTemplate" Value="{StaticResource ListBox_DataTemplate}" />
<Setter Property="ItemsPanel" Value="{StaticResource ListBox_HorizontalItems}" />
</Style>
</Window.Resources>
<Grid>
<ListBox Name="lbxImages" Style="{StaticResource ListBox_Style_Horizontal}" Width="250" Height="100" />
</Grid>
非常接近我需要的内容!但是我不知道如何减少新项添加到列表时显示的图片数量。目前,当添加新项时,会出现滚动条。如果我没有表达清楚,这里有一些截图展示我需要的功能:
能否有人向我展示如何实现这个功能?谢谢任何帮助!