Populating ItemsControl Using Uniform Grid as ItemsPanel Column First

我只是一个虾纸丫 提交于 2021-01-29 10:02:57

问题


I have an ObservableCollection of 24 Things.

   private ObservableCollection<Thing> things;
        public ObservableCollection<Thing> Things
        {
            get => things;
            set
            {
                things= value;
                RaisePropertyChanged();
            }
        }

I also have a selected Thing

   private Thing selectedThing;
        public Thing SelectedThing
        {
            get => selectedThing;
            set
            {
                selectedThing= value;
                RaisePropertyChanged();
            }
        }

I need to display these items in a grid. I am creating a grid of buttons, each with a command and a command parameter that allows me to set selected Thing from the collection.

I need to populate this grid COLUMNS first. I.E:

Is there a way to do this in WPF Using an ItemsControl and a Uniform grid?

   <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>

回答1:


In case a simple reordering of the elements in the ItemsSource collection is not possible, the following LayoutTransforms should do the job:

<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>


来源:https://stackoverflow.com/questions/61675602/populating-itemscontrol-using-uniform-grid-as-itemspanel-column-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!