WPF:对分组的 ICollectionView 使用不同的方向



>我有一个分组ListCollectionView显示在ListBox中。

    <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <StackPanel>
                        <Label Content="{Binding Path=Name}"/>
                        <WrapPanel Orientation="Horizontal">                                
                            <ItemsPresenter />
                        </WrapPanel>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ListBox ItemsSource="{Binding MyElementView, Mode=OneWay}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}" />
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type model:MyElementViewModel}">
                ...
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

现在所有元素都以垂直方式列出,但我想水平放置组,但组中的项目应该垂直列出。我怎么能意识到呢?

GroupStyle有一个Panel属性,您可以将其设置为ItemsPanelTemplate

<ListBox ...>
    <ListBox.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            ...
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

最新更新