wpf 如何修复列表框 xaml 项目顺序,从左到右,从上到下

wpf how to fix listbox xaml items order, from left to right, from top to bottom

我在我的项目中创建了一个从文件夹中获取图像的系统,并将其放入像 ItemsSource 一样的列表框中,我想这样安排:

1 2 3
4 5 6
7 8 9

如果我将 window 调整为大 window,将会发生以下情况:

1 2 3 4
5 6 7 8
9 .. ..

但是我在排列列表时遇到了问题。以及如何从 ItemsSource 获取文本 selectedItem 如何? 这里的代码:

<Window.Resources>

        <DataTemplate x:Key="TileTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="55"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Image Grid.Row="0" Source="Images/folder.png" Margin="10" Height="40" Width="35"/>
                <TextBlock Grid.Row="1" Margin="3,0,0,5" TextAlignment="Center" Text="{Binding Names}" Width="70"/>
            </Grid>
        </DataTemplate>

        <ItemsPanelTemplate x:Key="TilePanel">
            <WrapPanel/>
        </ItemsPanelTemplate>

        <Style TargetType="local:LayoutListBox">
            <Style.Triggers>
                <Trigger Property="ViewLayout" Value="Tile">
                    <Setter Property="ItemsPanel" Value="{StaticResource TilePanel}"/>
                    <Setter Property="ItemTemplate" Value="{StaticResource TileTemplate}"/>
                </Trigger>
            </Style.Triggers>
            
        </Style>
        
    </Window.Resources>

你可以这样做:

    <ListBox ItemsSource="{Binding YourList}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="3"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

然后即使你放大 window 你也会在每列中留下 3 项