在行和列中显示ItemsControl

时间:2018-08-30 14:37:49

标签: wpf xaml

我有以下XAML,它在一行中水平显示四个按钮。

如何使其每行显示三个按钮,并根据需要显示多少行?

XAML

    <ItemsControl ItemsSource="{Binding DataButtons}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"  Margin="20"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Width="100" Height="100"
                    Content="{Binding ButtonName}" 
                    Command="{Binding ButtonCommand}"
                    CommandParameter="{Binding FilePath}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我现在要得到什么

enter image description here

我需要什么

enter image description here

1 个答案:

答案 0 :(得分:0)

听起来像您想使用的是UniformGrid之类的“ ASh”,它可以让您将Columns设置为3,并自动缩放Rows的数量根据{{​​1}}中有多少项。我发现此方法比手动堆叠WrapPanels容易得多。