ListView with nested Expander not collapsing

蹲街弑〆低调 提交于 2019-12-04 04:14:07

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!