سؤال

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>
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

  1. In your expander's header, add a control of your choice that is able to show the 2 first items of your list.
  2. Bind the visibility of the above control to the "IsExpanded" proprty of the expander, so that the control is hidden when the expander is expanded (use IValueConverter)
  3. Build the content of your expander to show all of the items.

This way, when the expander is not expanded, it will show the 2 items in the header (style this to your liking). When the user expands, the 2 items disappear, the expander is expanded and the whole list is shown again.

Good luck!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top