Question

I have an Expander control inside of a Border control. When Expander is collapsed the Border retains of same height. How do I make the Border to change it's height also? The code that illustates the issue:

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.Resources>
        <Style TargetType="Border">
            <Setter Property="BorderBrush" Value="Red"/>
            <Setter Property="BorderThickness" Value="2" />
        </Style>
        <Style TargetType="StackPanel" x:Key="StackPanel1">
            <Setter Property="Orientation" Value="Vertical"/>
            <Setter Property="MaxHeight" Value="100" />
        </Style>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" x:Key="panelBackgroundBrush">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#FFE3EFFF" Offset="0" />
                <GradientStop Color="#FFAFD2FF" Offset="1" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <Style TargetType="{x:Type GridSplitter}">
            <Setter Property="FrameworkElement.Height" Value="3"/>
            <Setter Property="UIElement.SnapsToDevicePixels" Value="True" />
            <Setter Property="UIElement.Focusable" Value="False" />
            <Setter Property="FrameworkElement.Cursor" Value="SizeNS"/>
            <Setter Property="VerticalAlignment" Value="Top"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border>
                            <Border BorderThickness="0" BorderBrush="{StaticResource panelBackgroundBrush}">
                                <Canvas Width="19" Height="3">
                                    <Canvas.Resources>
                                        <Style TargetType="Rectangle">
                                            <Setter Property="Width" Value="2" />
                                            <Setter Property="Height" Value="2" />
                                            <Setter Property="Canvas.Top" Value="0" />
                                        </Style>
                                    </Canvas.Resources>
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="1" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="5" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="9" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="13" />
                                    <Rectangle Fill="{StaticResource panelBackgroundBrush}" Canvas.Left="17" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="0" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="4" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="8" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="12" />
                                    <Rectangle Fill="{TemplateBinding TextElement.Foreground}" Canvas.Left="16" />
                                </Canvas>
                            </Border>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <Border>
        <Expander>
            <Border>
                <StackPanel Style="{StaticResource ResourceKey=StackPanel1}" >
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                </StackPanel>
            </Border>
        </Expander>
    </Border>
    <Border Grid.Row="1">
        <Expander>
            <Border>
                <StackPanel Style="{StaticResource ResourceKey=StackPanel1}" >
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                </StackPanel>
            </Border>
        </Expander>
    </Border>
    <GridSplitter Grid.Row="1" />
    <Border Grid.Row="2">
        <Expander>
            <Border>
                <StackPanel Style="{StaticResource ResourceKey=StackPanel1}" >
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                    <TextBlock Text="aaaa"/>
                </StackPanel>
            </Border>
        </Expander>
    </Border>
    <GridSplitter Grid.Row="2" />
</Grid>
Was it helpful?

Solution

To solve the issue RowDefinition.Height had to be set to Auto.

OTHER TIPS

The best way to do so is to set the border to be a child of a dockpanel and set the LastChildFill property of the dockpanel to be true. Here is an example:

<DockPanel LastChildFill="True">
    <Border>
        <Expander Name="PropertiesExpander2"
                  ExpandDirection="Left"
                  IsExpanded="False"
                  DockPanel.Dock="Right">
         </Expander>
    </Border>

    <Border>
              //Here you can add other controls
    </Border>
    </DockPanel>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top