You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF实现ListView分组默认折叠并支持手动展开

Solution: Set Default Collapsed Groups for DetailsListView

Got it, let's get your grouped list view to default to collapsed groups while keeping manual expand functionality intact. Here are two straightforward approaches you can use:

Approach 1: XAML Style (Simplest Option)

If your custom abc:DetailsListView uses standard WPF GroupItem controls for grouping headers, you can set a default style to collapse all groups right in your XAML. Just add a resource section to your list view:

<abc:DetailsListView x:Name="EncumbranceEntriesList" Grid.Row="1" ItemsSource="{Binding CurrentEncumbranceJournalEntries}" SelectionMode="Extended" ShowGroupItemCount="True" IsSortingEnabled="False" abc:ListBoxExtensions.HasBindableSelectedItems="True">
    <!-- Add this style to set default collapsed state -->
    <abc:DetailsListView.Resources>
        <Style TargetType="GroupItem">
            <Setter Property="IsExpanded" Value="False"/>
        </Style>
    </abc:DetailsListView.Resources>

    <ListView.View>
        <abc:DetailsView>
            <abc:DetailsViewColumn Header="PO #" Width="100" IsInitiallyVisible="False" IsUserHidable="False" DisplayMemberBinding="{Binding PurchaseOrderDetail.PurchaseOrder.DisplayNumber}" SortPropertyName="PurchaseOrderDetail.PurchaseOrder.DisplayNumber" GroupPropertyName="PurchaseOrderDetail.PurchaseOrder.DisplayNumber"/>
            <abc:DetailsViewColumn Header="Line Item" Width="200" DisplayMemberBinding="{Binding PurchaseOrderDetail.ShortItemDescription}"/>
            <abc:DetailsViewColumn Header="Encumbrance Date" Width="120" DisplayMemberBinding="{Binding EntryDate, StringFormat=\{0:d\}}" SortPropertyName="EntryDate" DefaultSortDirection="Descending"/>
        </abc:DetailsView>
    </ListView.View>
</abc:DetailsListView>

This style automatically applies to every group header, setting their default state to collapsed. Users can still click any group header to expand or collapse it manually—no extra code needed for that interaction.

Approach 2: Code-Behind (For Custom Control Compatibility)

If the style approach doesn't work with your custom DetailsListView, you can programmatically collapse groups after the UI has finished rendering.

First, add this helper method to your code-behind:

private void CollapseAllGroups()
{
    foreach (var group in EncumbranceEntriesList.Items.Groups)
    {
        var groupItem = EncumbranceEntriesList.ItemContainerGenerator.ContainerFromItem(group) as GroupItem;
        if (groupItem != null)
        {
            groupItem.IsExpanded = false;
        }
    }
}

Then update your existing grouping code to call this method after a short delay (to ensure the UI has created all group controls):

ICollectionView dataView = CollectionViewSource.GetDefaultView(EncumbranceEntriesList.ItemsSource);
if (dataView != null && dataView.SortDescriptions.Count == 0)
{
    dataView.SortDescriptions.Add(new SortDescription("PurchaseOrderDetail.PurchaseOrder.DisplayNumber", ListSortDirection.Ascending));
    dataView.SortDescriptions.Add(new SortDescription("PurchaseOrderDetail.TransactionDate", ListSortDirection.Ascending));
    dataView.SortDescriptions.Add(new SortDescription("EntryDate", ListSortDirection.Ascending));
    EncumbranceEntriesList.Group("PurchaseOrderDetail.PurchaseOrder.DisplayNumber");
    
    // Wait for UI to load before collapsing groups
    EncumbranceEntriesList.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, 
        new Action(() => CollapseAllGroups()));
}

Quick Notes:

  • The Dispatcher.BeginInvoke ensures we only try to access group controls after they've been rendered by the UI—this avoids null reference errors.
  • Both methods keep manual expand/collapse functionality fully intact, since that's a built-in feature of the GroupItem control.

内容的提问来源于stack exchange,提问作者newbee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:12:12