Как отображать группы в ListView внутри горизонтальной StackPanel?

У меня есть ListView, который используется для отображения элементов с одним уровнем группировки, но мне не удалось показать группы в горизонтальной StackPanel, я установил шаблон ListView, его свойство ItemsPanel, его свойство ItemTemplate и его свойство GroupStyle но он не отображается, как на изображении ниже.

Я прочитал этот пост и не понял, как применить полученные знания в моем случае.

XAML

<SolidColorBrush x:Key="ListBox.Static.Background" Color="#FFFFFFFF"/>
<SolidColorBrush x:Key="ListBox.Static.Border" Color="#FFABADB3"/>
<SolidColorBrush x:Key="ListBox.Disabled.Background" Color="#FFFFFFFF"/>
<SolidColorBrush x:Key="ListBox.Disabled.Border" Color="#FFD9D9D9"/>
<Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}">
    <Setter Property="Background" Value="{StaticResource ListBox.Static.Background}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ListBox.Static.Border}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListView}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
                    <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
                        <StackPanel Orientation="Horizontal">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </StackPanel>
                    </ScrollViewer>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource ListBox.Disabled.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ListBox.Disabled.Border}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsGrouping" Value="true"/>
                            <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

(тогда нерелевантная разметка здесь :)

<ListView Background="Green"
    Foreground="White" VerticalAlignment="Center"
    ItemsSource="{x:Static local:GameLevels.Levels}" Name="LevelsListBox" HorizontalAlignment="Center" Style="{DynamicResource ListViewStyle1}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid HorizontalAlignment="Left" VerticalAlignment="Top">
            </UniformGrid>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" TextAlignment="Center"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Padding="5,0,0,0" FontWeight="Bold" FontSize="14" Text="{Binding Name}" Visibility="{Binding Name, Converter={StaticResource IsGroupTitleInChapter0Conv}}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

Код программной части

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(LevelsListBox.ItemsSource);
PropertyGroupDescription gd = new PropertyGroupDescription("Category");
view.GroupDescriptions.Add(gd);

Фактический снимок экрана

фактический

Предполагаемый снимок экрана

ожидаемый


person silviubogan    schedule 30.06.2019    source источник


Ответы (1)


Вы должны установить свойство Panel для GroupStyle:

<ListView Background="Green"
    Foreground="White" VerticalAlignment="Center"
    ItemsSource="{x:Static local:GameLevels.Levels}" Name="LevelsListBox" HorizontalAlignment="Center">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" TextAlignment="Center"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Padding="5,0,0,0" FontWeight="Bold" FontSize="14" Text="{Binding Name}" Visibility="{Binding Name, Converter={StaticResource IsGroupTitleInChapter0Conv}}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>
person mm8    schedule 01.07.2019