【问题标题】:Populating ItemsControl Using Uniform Grid as ItemsPanel Column First首先使用统一网格作为 ItemsPanel 列填充 ItemsControl
【发布时间】:2020-05-08 09:01:28
【问题描述】:

我有一个包含 24 个事物的 ObservableCollection。

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

我也有一个选定的东西

   private Thing selectedThing;
        public Thing SelectedThing
        {
            get => selectedThing;
            set
            {
                selectedThing= 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>

【问题讨论】:

  • 只需对 Things 中的元素重新排序即可。

标签: c# wpf mvvm itemscontrol


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多