Bogdan Verbenets
Bogdan Verbenets

Reputation: 26956

items are not displayed after grouping in ItemsControl

I've added grouping to ItemsControl:

        <ItemsControl Style="{StaticResource SellingDashboardToDosList}" Grid.Row="2" BorderThickness="1" Background="#C7E8F8" HorizontalAlignment="Stretch" ItemsSource="{Binding ToDoList}" >
            <ItemsControl.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="GroupItem">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="GroupItem">
                                        <GroupBox Header="{Binding Name}">
                                            <ItemsPresenter />
                                        </GroupBox>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ItemsControl.GroupStyle>
        </ItemsControl>

Now I see only empty GroupBoxes. I've used Snoop tool to explore the application and I found out that GroupBox ItemPresenters are empty! What could be the reason of it?

If I remove the grouping from the ItemsControl (the ItemsControl.GroupStyle element), then everything works fine, and I see all items again. I don't need to make any changes to the underlying data context to see all items. The data context (the ItemsSource binging) is of type CollectionViewSource.

The binding tracing is turned on, but I don't see any binding errors.

Upvotes: 0

Views: 1240

Answers (2)

Bogdan Verbenets
Bogdan Verbenets

Reputation: 26956

It appears that ItemsControl style was overriding ItemsControl.Template property. The problem was solved once that style got overridden.

Upvotes: 1

Sergei B.
Sergei B.

Reputation: 3227

You have to group your data first. Use CollectionViewSource to do that:

<CollectionViewSource x:Key="Data" Source="{StaticResource SellingDashboardToDosList}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="PropertyNameToGroupBy"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

And only then you can do the following:

<ItemsControl ItemsSource="{Biding Source={StaticResource Data}}" ...

Upvotes: 0

Related Questions