WPF grid expander listview fills up the space when gridsplitter moves

允我心安 提交于 2019-12-12 10:25:59

问题


I have the following .XAML:

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Expander Grid.Row="0">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="5"/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0"/>
        <GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" ShowsPreview="true" ResizeDirection="Rows" Height="5"/>
   </Grid>
</Expander>
<Expander Grid.Row="1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="5"/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0"/>
        <GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" ShowsPreview="true" ResizeDirection="Rows" Height="5"/>
   </Grid>
</Expander>

There are 2 expanders with gridsplitters. I want to achieve the following 2 things:

(1) Whenever one expander collapses, the other expander should fill up the space

(2) Whenever one gridsplitter moves, the 2 expanders automatically adjusts their heights to fill up the space.

The behavior is expected to be similar to the Windows Resource Manager Overview window's behavior. Any advice and insight is appreciated


回答1:


See if this is what you need :

<Window ...>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Expander Grid.Row="0" Collapsed="Expander_Collapsed_1">
        <ListView x:Name="Lv1"/>
    </Expander>
    <GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" ShowsPreview="true" ResizeDirection="Rows" Height="5" Background="#FFB82424"/>

    <Expander Grid.Row="2" Collapsed="Expander_Collapsed_1">
        <ListView x:Name="Lv2" Grid.Row="0"/>          
    </Expander>
    <GridSplitter Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Top" ShowsPreview="true" ResizeDirection="Rows" Height="5" Background="#FFC51A1A"/>
</Grid>
</Window>

Code :

private void Expander_Collapsed_1(object sender, RoutedEventArgs e)
{
    DependencyObject dobj = VisualTreeHelper.GetParent(sender as Expander);
    while (!(dobj is Grid))
        dobj = VisualTreeHelper.GetParent(dobj);

    int i = Grid.GetRow(sender as Expander);
    Grid grd = dobj as Grid;
    grd.RowDefinitions[i].Height = GridLength.Auto;
}


来源:https://stackoverflow.com/questions/39917247/wpf-grid-expander-listview-fills-up-the-space-when-gridsplitter-moves

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