问题
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