C# 首先使用统一网格作为ItemsPanel列填充ItemsControl

C# 首先使用统一网格作为ItemsPanel列填充ItemsControl,c#,wpf,mvvm,itemscontrol,C#,Wpf,Mvvm,Itemscontrol,我收集了24件东西 private ObservableCollection<Thing> things; public ObservableCollection<Thing> Things { get => things; set { things= value; RaisePropertyC

我收集了24件东西

   private ObservableCollection<Thing> things;
        public ObservableCollection<Thing> Things
        {
            get => things;
            set
            {
                things= value;
                RaisePropertyChanged();
            }
        }
我需要在网格中显示这些项目。我正在创建一个按钮网格,每个按钮都有一个命令和一个命令参数,允许我从集合中设置所选内容

我需要先填充这个网格列。即:

有没有一种方法可以在WPF中使用ItemsControl和统一网格来实现这一点

   <ItemsControl ItemsSource="{Binding Things}" HorizontalAlignment="Center" Margin="20">

            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                   <UniformGrid Columns="3" Rows="8"  />




                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
                <DataTemplate>



                    <Button Content="{Binding ThingPosition}" 
                        Height="30"
                        Width="80"
                            Margin="3"
                        FontSize="8"
                        Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.SelectThingCommand}" 
                        CommandParameter="{Binding Path=.}"/>



                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

如果无法对ItemsSource集合中的元素进行简单的重新排序,则应使用以下布局转换:

<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="3">
                <UniformGrid.LayoutTransform>
                    <TransformGroup>
                        <RotateTransform Angle="-90"/>
                        <ScaleTransform ScaleY="-1"/>
                    </TransformGroup>
                </UniformGrid.LayoutTransform>
            </UniformGrid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <TransformGroup>
                        <ScaleTransform ScaleY="-1"/>
                        <RotateTransform Angle="90"/>
                    </TransformGroup>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        ...
    </ItemsControl.ItemTemplate>
</ItemsControl>

...

只需重新排列事物中的元素即可。
<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="3">
                <UniformGrid.LayoutTransform>
                    <TransformGroup>
                        <RotateTransform Angle="-90"/>
                        <ScaleTransform ScaleY="-1"/>
                    </TransformGroup>
                </UniformGrid.LayoutTransform>
            </UniformGrid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <TransformGroup>
                        <ScaleTransform ScaleY="-1"/>
                        <RotateTransform Angle="90"/>
                    </TransformGroup>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        ...
    </ItemsControl.ItemTemplate>
</ItemsControl>