I recently had to do something similar. I used very similar code for what you have, but I was able to achieve the desired result using the code page by page. Try something like this:
WPF
<Grid Grid.Row="1" Name="pageGrid" Margin="0,10,0,0"> <Grid.RowDefinitions> <RowDefinition MinHeight="25" Height="*" /> <RowDefinition MinHeight="25" Height="*" /> <RowDefinition MinHeight="25" Height="*" /> <RowDefinition MinHeight="25" Height="*" /> <RowDefinition MinHeight="25" Height="*" /> <RowDefinition MinHeight="25" Height="*" /> <RowDefinition MinHeight="25" Height="*" /> </Grid.RowDefinitions> <Expander Grid.Row="0" Header="header1" Name="expander1" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="18" IsExpanded="True"> <Grid> <TextBlock Background="#336699FF" Padding="5" TextWrapping="Wrap" Margin="30,5,10,5"> test </TextBlock> </Grid> </Expander> <Expander Grid.Row="1" Header="header2" Margin="0,0,0,0" Name="expander2" VerticalAlignment="Top" FontSize="18"> <Grid> <TextBlock Background="#336699FF" Padding="5" TextWrapping="Wrap" Margin="30,5,10,5"> test </TextBlock> </Grid> </Expander> <Expander Grid.Row="2" Header="header3" Margin="0,0,0,0" Name="expander3" VerticalAlignment="Top" FontSize="18"> <Grid> <TextBlock Background="#336699FF" Padding="5" TextWrapping="Wrap" Margin="30,5,10,5"> test </TextBlock> </Grid> </Expander> <Expander Grid.Row="3" Header="header4" Margin="0,0,0,0" Name="expander4" VerticalAlignment="Top" FontSize="18"> <Grid> <TextBlock Background="#336699FF" Padding="5" TextWrapping="Wrap" Margin="30,5,10,5"> test </TextBlock> </Grid> </Expander> <Expander Grid.Row="4" Header="header5" Margin="0,0,0,0" Name="expander5" VerticalAlignment="Top" FontSize="18"> <Grid> <TextBlock Background="#336699FF" Padding="5" TextWrapping="Wrap" Margin="30,5,10,5"> test </TextBlock> </Grid> </Expander> <Expander Grid.Row="5" Header="header6" Margin="0,0,0,0" Name="expander6" VerticalAlignment="Top" FontSize="18"> <Grid> <TextBlock Background="#336699FF" Padding="5" TextWrapping="Wrap" Margin="30,5,10,5"> test </TextBlock> </Grid> </Expander> <Expander Grid.Row="6" Header="header7" Margin="0,0,0,0" Name="expander7" VerticalAlignment="Top" FontSize="18"> <Grid> <TextBlock Background="#336699FF" Padding="5" TextWrapping="Wrap" Margin="30,5,10,5"> text </TextBlock> </Grid> </Expander> </Grid>
In the window code behind me, I used C # and had this code:
FROM#
/// <summary> /// Interaction logic for _07Slide.xaml /// </summary> public partial class _07Slide : Page { GridLength[] starHeight; public _07Slide() { InitializeComponent(); starHeight = new GridLength[pageGrid.RowDefinitions.Count]; starHeight[0] = pageGrid.RowDefinitions[0].Height; starHeight[1] = pageGrid.RowDefinitions[2].Height; starHeight[2] = pageGrid.RowDefinitions[2].Height; starHeight[3] = pageGrid.RowDefinitions[2].Height; starHeight[4] = pageGrid.RowDefinitions[2].Height; starHeight[5] = pageGrid.RowDefinitions[2].Height; starHeight[6] = pageGrid.RowDefinitions[2].Height; ExpandedOrCollapsed(expander1); ExpandedOrCollapsed(expander2); ExpandedOrCollapsed(expander3); ExpandedOrCollapsed(expander4); ExpandedOrCollapsed(expander5); ExpandedOrCollapsed(expander6); ExpandedOrCollapsed(expander7); expander1.Expanded += ExpandedOrCollapsed; expander1.Collapsed += ExpandedOrCollapsed; expander2.Expanded += ExpandedOrCollapsed; expander2.Collapsed += ExpandedOrCollapsed; expander3.Expanded += ExpandedOrCollapsed; expander3.Collapsed += ExpandedOrCollapsed; expander4.Expanded += ExpandedOrCollapsed; expander4.Collapsed += ExpandedOrCollapsed; expander5.Expanded += ExpandedOrCollapsed; expander5.Collapsed += ExpandedOrCollapsed; expander6.Expanded += ExpandedOrCollapsed; expander6.Collapsed += ExpandedOrCollapsed; expander7.Expanded += ExpandedOrCollapsed; expander7.Collapsed += ExpandedOrCollapsed; } void ExpandedOrCollapsed(object sender, RoutedEventArgs e) { ExpandedOrCollapsed(sender as Expander); } void ExpandedOrCollapsed(Expander expander) { var rowIndex = Grid.GetRow(expander); var row = pageGrid.RowDefinitions[rowIndex]; if (expander.IsExpanded) { row.Height = starHeight[rowIndex]; row.MinHeight = 25; } else { starHeight[rowIndex] = row.Height; row.Height = GridLength.Auto; row.MinHeight = 0; } } }
In this example, all expanders will grow to completely fill the grid. If you want, you can change this to collapse other expanders when it is selected.