在水平方向对齐 WPF ListBox 中的项目

Align items in WPF ListBox in horizontal direction

我想让我添加的图像水平对齐,但我尝试的任何操作都会使项目垂直对齐

所以我的项目是这样排列的 ->

但我想要这样的东西 ->

这是我试过的 ->

<ListBox x:Name="listbox" ScrollViewer.VerticalScrollBarVisibility="Visible" Grid.Row="1">
    <ItemsPanelTemplate>
        <StackPanel HorizontalAlignment="Stretch">
            <StackPanel.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                </Style>
            </StackPanel.Resources>
        </StackPanel>
    </ItemsPanelTemplate>
    <ListBox.ItemTemplate >
        <DataTemplate >
            <WrapPanel Orientation="Horizontal" >
                <Image  Height="100"  Width="100" Stretch="Fill" Source="{Binding ConvertedData}" />
            </WrapPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

在 ItemsPanelTemplate 中使用 WrapPanel。 ItemsPanel 是负责 ItemsControl 内项目布局的面板。 ItemTemplate 中的面板控制单个项目的布局。

还可以使用 ItemContainerStyle 修改 ListBoxItems 的属性。

<ListBox x:Name="listbox" ScrollViewer.VerticalScrollBarVisibility="Visible" Grid.Row="1">
   <ListBox.ItemsPanel>
       <ItemsPanelTemplate>
          <WrapPanel Orientation="Horizontal"/>
       </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
             <Image  Height="100"  Width="100" Stretch="Fill" Source="{Binding ConvertedData}" /> 
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>