Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the groups of a grouped CollectionView be presented horizontally?

I'm implementing a ListBox whose ItemsPanel is a WrapPanel as per this answer, but there's a twist: my ItemsSource is a grouped CollectionView. With a GroupStyle applied to my ListBox, the wrapping shown in that question doesn't work: the groups are always displayed vertically.

Snooping on my app, here's why:

Snoop displaying WrapPanel in GroupItem

As you can see, the WrapPanel, defined as my ListBox's ItemsPanelTemplate, appears in the ItemsPresenter within each GroupItem; an implicit, vertically-oriented StackPanel (top item in the pink box) is created to contain the GroupItems themselves.

Is there a way to override this behavior, so the GroupItems are placed in a WrapPanel? Would I have to re-template the entire ListBox?

Update: To illustrate what I'm actually doing with my ListBox and the CollectionView grouping, let me post a little XAML:

<Grid>
    <ListBox ItemsSource="{Binding}"                 
             ScrollViewer.VerticalScrollBarVisibility="Disabled"
             SelectionMode="Multiple"
             ItemContainerStyle="{StaticResource itemStyle}">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type WpfApplication1:Item}">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Name}" FontSize="10"/>
                    <TextBlock Text="{Binding Amount, StringFormat={}{0:C}}" FontSize="10"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Grid>

The GroupStyle is at the heart of it: if you remove that, the GroupItems don't get rendered, and the WrapPanel (which you can see appearing beneath the GroupItem in the screenshot above) appears in place of (StackPanel) 98 in the screenshot.

like image 923
Dan J Avatar asked Mar 31 '11 18:03

Dan J


1 Answers

This behavior only seems to occur if you have defined a HeaderTemplate in the GroupStyle.

It can be corrected by setting the GroupStyle.Panel property to contain a WrapPanel:

<ListBox.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
        <GroupStyle.Panel>
            <ItemsPanelTemplate>
                <WrapPanel></WrapPanel>
            </ItemsPanelTemplate>
        </GroupStyle.Panel>
    </GroupStyle>
</ListBox.GroupStyle>

It will look something like this:
enter image description here

like image 87
Greg Sansom Avatar answered Oct 23 '22 11:10

Greg Sansom