WPF Listbox grouping

2019-05-13 21:28发布

问题:

I need to display in the list box like below. The scenario is it will have multiple groups followed by items with alignment horizontally.

    GroupA
      GroupA Description
    GroupB
      GroupB Description
    Items Available
     ItemA ItemB ITemC

回答1:

You can try with this code

<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Expander Header="{Binding ....}" IsExpanded="True">
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
</Style>


<ListBox x:Name="lbPersonList" Margin="19,17,162,25" AlternationCount="2">
            <ListBox.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
            </ListBox.GroupStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ...}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>

Nota : adjust your binding on your code