开发者

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:

Can the groups of a grouped CollectionView be presented horizontally?

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 r开发者_JAVA技巧endered, and the WrapPanel (which you can see appearing beneath the GroupItem in the screenshot above) appears in place of (StackPanel) 98 in the screenshot.


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:

Can the groups of a grouped CollectionView be presented horizontally?


You should be able to replace the group style from the items control (ListBox):

<ListBox.GroupStyle>
    <Style />
<ListBox.GroupStyle/>

Or, you should also be able to create a DataTemplate based on the group item object:

<DataTemplate DataType="{x:Type GroupItem}">
    <Panel />
</DataTemplate>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜