将 ItemsPanelTemplate 绑定到 ItemsSource 的 属性

Bind ItemsPanelTemplate to property of ItemsSource

我正在尝试创建一个按钮数量可变的自定义 MessageBox,该数量等于 ItemsSource 的计数。这些按钮将放置在 UniformGrid 中。 ButtonView 的 ItemsSource 分配给 ObservableCollection Buttons。 Buttons.Count 未被代码识别。我怎样才能正确分配 UniformGrid 列值?

    <ItemsControl Grid.Row="2" x:Name="ButtonView">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="{Binding Buttons.Count}">
                </UniformGrid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="Button">
                <Button Margin="2" Content="{Binding Content}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

设置 Rows 属性 而不是 Columns:

<ItemsPanelTemplate>
    <UniformGrid Rows="1"/>
</ItemsPanelTemplate>