简体   繁体   English

如何使WPF扩展器在垂直StackPanel中垂直拉伸

[英]How can you make a WPF Expander stretch vertically within a vertical StackPanel

Given the XAML below, is it possible to make an adjustment so that when either of the Expander items are expanded, the TextBlock content stretches vertically to fill the remaining space? 给定下面的XAML,是否可以进行调整,以便在Expander任何一个扩展时, TextBlock内容都可以垂直拉伸以填充剩余空间? (Note that this XAML is purposely simplified, the solution must satisfy this simple example and any data bound ItemsSource scenario). (请注意,此XAML是有意简化的,解决方案必须满足此简单示例以及任何与数据绑定的ItemsSource方案)。

The StackPanel itself stretches to fill the space (since it is contained within a Grid ), so I assume there is a conflict within the StackPanel measuring code that does not allow infinitely large content elements. StackPanel本身会拉伸以填充空间(因为它包含在Grid ),因此我假设StackPanel 测量代码中存在冲突,该冲突不允许无限大的内容元素。 I'm not even sure if this is possible to resolve elegantly, it may just require manually overriding the measuring code and determining the optimal expanded height for the content container. 我什至不确定这是否可以优雅地解决,它可能只需要手动覆盖测量代码并确定内容容器的最佳扩展高度即可。

<Grid>
    <StackPanel>
        <Expander Header="Item 1">
            <TextBlock Background="LightCoral" Text="Content 1 should be stretched vertically when expanded"/>
        </Expander>
        <Expander Header="Item 2">
            <TextBlock Background="LightBlue" Text="Content 2 should be stretched vertically when expanded"/>
        </Expander>
    </StackPanel>
</Grid>

NOTE : I read through How to get StackPanel's children to fill maximum space downward? 注意 :我通读了如何使StackPanel的子级向下填充最大空间? and it is not really related to this question as the solution to that post is to use a DockPanel which is not possible in a data bound ItemsSource scenario. 并且与该问题并没有真正的关系,因为该帖子的解决方案是使用DockPanel ,这在数据绑定的ItemsSource场景中是不可能的。

StackPanel sets the Content Height to auto even if it is placed inside a Grid control. 即使将其放置在Grid控件中,StackPanel也会将“内容高度”设置为自动。 'Grid' should be used in place of StackPanel and then set Row height from code behind. 应使用“网格”代替StackPanel,然后从后面的代码中设置行高。 Here is an example; 这是一个例子。

       <Grid>

            <Grid.RowDefinitions>
                <RowDefinition x:Name="row1" Height="auto"/>
                <RowDefinition x:Name="row2" Height="auto"/>
            </Grid.RowDefinitions>
            <Expander Header="Item 1"  Expanded="Expander_Expanded_1" VerticalAlignment="Stretch" >               
                    <TextBlock Background="LightCoral" Text="Content 1 should be stretched vertically when expanded" VerticalAlignment="Stretch"/>              
            </Expander>
            <Expander Header="Item 2" Grid.Row="1" Expanded="Expander_Expanded_2" VerticalAlignment="Stretch" >               
                    <TextBlock Background="LightBlue" Text="Content 2 should be stretched vertically when expanded" VerticalAlignment="Stretch"/>             
            </Expander>

        </Grid>

And in the code behind you can set the height of the row on expanded event 在后面的代码中,您可以在扩展事件上设置行的高度

private void Expander_Expanded_1(object sender, RoutedEventArgs e)
        {
            ex2.Height = new GridLength(1, GridUnitType.Auto);
            ex1.Height = new GridLength(1, GridUnitType.Star);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM