Search code examples
wpfdatagridexpandergroupstyle

wpf datagrid expander show x items


I have an Expander in my DataGrid, but I want to only show TWO items, and then when user click on expand, show remaining items.

How can that be done ?

<DataGrid.GroupStyle>
    <GroupStyle AlternationCount="7" >
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander IsExpanded="False" Background="{Binding XPath=recipient_color}">                                            
                                <Expander.Header>
                                    <Label Content="{Binding}">
                                    </Label>                                                
                                </Expander.Header>
                                <Expander.Content>
                                    <ItemsPresenter/>
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

Solution

  • Another way to do it that would require more fiddling around with the xaml though, would be to modify the Expander control template. In the control template on MSDN, you can see that the grid row that's named ContentRow. The height of that row starts at 0, then a trigger on the IsExpanded property expands it to its desired height. If you set the default ContentRow height to something like 50 (or whatever height is needed to show the number of items you want), it will show the top part of the group's items when the Expander is collapsed.

    You can read up more on how to modify ControlTemplates here.