列表框显示水平图像WPF

qu1*_*dry 3 wpf xaml listbox image

我正在尝试在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>
Run Code Online (Sandbox Code Playgroud)

这与我的需求非常接近!但是我无法弄清楚如何在将新项目添加到列表时减少显示的图像数量.目前,添加新项目时会出现滚动条.Incase我不是很好地解释自己这里有一些屏幕截图显示我需要的功能:

3项 4项 5项

任何人都可以告诉我如何实现这一目标吗?谢谢你的帮助!

Mar*_*nov 9

使用以下UniformGrid作为ItemsPanel:

<ItemsPanelTemplate>
    <UniformGrid Columns="{Binding Path=Items.Count,RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"/>
</ItemsPanelTemplate>
Run Code Online (Sandbox Code Playgroud)

禁用水平滚动:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
Run Code Online (Sandbox Code Playgroud)

修改ItemTemplate:

<DataTemplate>
    <Image Source="{Binding ImageSource}"
           Stretch="None"
           HorizontalAlignment="Center"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)