ListView with nested Expander not collapsing ListView with nested Expander not collapsing wpf wpf

ListView with nested Expander not collapsing


Found the solution, and it details where in the question already.

ItemControl accepts an ItemsSource and auto resizes. So replacing the two ListViews with ItemControls gets the nested collapsing.

But there is no scrolling, so wrapping the outer ItemControl with a ScrollViewer, reproduces the complete desired effect.

<ScrollViewer    VerticalScrollBarVisibility="Auto">    <ItemsControl>        <!-- ParameterGroupView -->        <Border            BorderBrush="Brown"            BorderThickness="1"            CornerRadius="4"            Padding="4"            Height="Auto">            <ItemsControl                HorizontalContentAlignment="Stretch">                <Expander                    Header="Expander A"                    IsExpanded="False">                    <ItemsControl                        HorizontalContentAlignment="Stretch">                        <!-- TextView -->                        <TextBlock>Content A</TextBlock>                        <TextBlock>Content B</TextBlock>                    </ItemsControl>                </Expander>            </ItemsControl>        </Border>    </ItemsControl></ScrollViewer>

I was testing with double Expanders in the Border and double Border sections.


The most obvious thing to do here is to put the expanders in a container other than a listview:

<Border BorderBrush="Brown" BorderThickness="1" CornerRadius="4" Padding="4">    <StackPanel>        <Expander Header="Expander A" IsExpanded="False">            <ListView HorizontalContentAlignment="Stretch" MinWidth="100">                <ListBox Name="listb"></ListBox>                <!-- TextView -->                <TextBlock >Content A</TextBlock>                <TextBlock>Content B</TextBlock>            </ListView>        </Expander>    </StackPanel></Border>

The container resizes around content just fine.

If you absolutely must have it in a ListView (which is possible) then I don't know of a way to get a ListView to resize easily once it's grown (beyond setting explicit sizes of the whole thing, which is clumsy and not useful). If thats the case then it's possible that you'll have to use a controllable listbox to display all the open expanders, or display the content in a different way (like in a popup, maybe?) if you want to be able to see it all at a glance.