下面顯示的代碼片段可能適用於您的場景。我能夠使用它來擴展/摺疊所有組和子組。這適用於我們的DataVirtualization示例和不使用數據虛擬化的網格。另外,我不必首先向下滾動,即使行數很多。
private void btnCollapseAllGroups_ButtonClick(object sender, RoutedEventArgs e)
{
CollapseOrExpandAll(null, true);
}
private void btnExpandAllGroups_ButtonClick(object sender, RoutedEventArgs e)
{
CollapseOrExpandAll(null, false);
}
private void CollapseOrExpandAll(CollectionViewGroup inputGroup, Boolean bCollapseGroup)
{
IList<Object> groupSubGroups = null;
// If top level then inputGroup will be null
if (inputGroup == null)
{
if (grid.Items.Groups != null)
groupSubGroups = grid.Items.Groups;
}
else
{
groupSubGroups = inputGroup.GetItems();
}
if (groupSubGroups != null)
{
foreach (CollectionViewGroup group in groupSubGroups)
{
// Expand/Collapse current group
if (bCollapseGroup)
grid.CollapseGroup(group);
else
grid.ExpandGroup(group);
// Recursive Call for SubGroups
if (!group.IsBottomLevel)
CollapseOrExpandAll(group, bCollapseGroup);
}
}
}