wpf datagrid扩展器显示x项

时间:2012-12-21 13:39:24

标签: wpf datagrid expander groupstyle

我的Expander中有DataGrid,但我只想显示两个项目,然后当用户点击展开时,显示剩余的项目。

怎么办呢?

<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>

2 个答案:

答案 0 :(得分:0)

  1. 在您的展开程序标题中,添加您选择的控件,该控件可以显示列表中的2个第一项。
  2. 将上述控件的可见性绑定到扩展器的“IsExpanded”属性,以便在展开扩展器时隐藏控件(使用IValueConverter)
  3. 构建展开器的内容以显示所有项目。
  4. 这样,当扩展器未展开时,它将在标题中显示2个项目(根据您的喜好设置样式)。当用户展开时,2个项目消失,扩展器展开,整个列表再次显示。

    祝你好运!

答案 1 :(得分:0)

另一种方法是,需要更多地摆弄xaml,就是修改Expander控件模板。在MSDN上的控件模板中,您可以看到名为ContentRow的网格行。该行的高度从0开始,然后IsExpanded属性上的触发器将其展开到所需的高度。如果您将默认ContentRow高度设置为50(或显示所需项目数所需的任何高度),则会在展开器折叠时显示该组项目的顶部。

您可以详细了解如何修改ControlTemplates here

相关问题