David Shochet
David Shochet

Reputation: 5375

Expander not stretchable

I have a WPF application, and I use expanders. When I make the expander collapse, it keeps occupying the space. Is it possible to make it free the space, in xaml?

Here is some code:

   <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="3*"></RowDefinition>
        <RowDefinition Height="4*"></RowDefinition>
        <RowDefinition Height="2*"></RowDefinition>
  </Grid.RowDefinitions>
    <wpfx:BusyIndicator Name="BusyBar" IsBusy="{Binding IsBusy}" BusyContent="Uploading enrollment data..." Grid.Row="0" />

    <Expander Grid.Row="1" IsExpanded="True" Header="Enrollment Files Upload">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal" Margin="3, 10" Grid.Row="0">
                <TextBlock Text="Agencies:" VerticalAlignment="Center" />
                <ComboBox x:Name="Agencies" DisplayMemberPath="PrimaryName" SelectedValuePath="AgentId" 
                      SelectedItem="{Binding SelectedAgency}"
                      MinWidth="100" Margin="3,0,10,0" VerticalAlignment="Center" />
                <TextBlock Text="Worksheet:" VerticalAlignment="Center" />
                <TextBox x:Name="WorkSheetName" VerticalAlignment="Center" Margin="3,0,10,0" />
                <Button x:Name="UploadFile"
                MinWidth="70"
                Margin="2"
                HorizontalAlignment="Center"
                VerticalAlignment="Center" 
                attachProperties:ButtonIcon.Icon="Resources/Images/add.png"
                Content="Upload File"  IsEnabled="{Binding EnrollmentFiles.Any}"                           
                Style="{StaticResource ImageButtonStyle}" />

                <Button x:Name="EnrollmentDelete"
                    MinWidth="70"
                    Margin="2"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center" 
                    attachProperties:ButtonIcon.Icon="Resources/Images/Remove.png"
                    Content="Delete Enrollment"  IsEnabled="{Binding EnrollmentFiles.Any}"                           
                    Style="{StaticResource ImageButtonStyle}" />
            </StackPanel>

            <c1:C1FlexGrid x:Name="EnrollmentFiles" Grid.Row="1"
                AutoGenerateColumns="False"
                IsReadOnly="True" Width="Auto"
                ItemsSource="{Binding Path=EnrollmentFiles}"
                SelectionMode="Row" KeepCurrentVisible="True"
                SelectedItem="{Binding Path=SelectedEnrollmentFile, Mode=TwoWay}">
...
            </c1:C1FlexGrid>
        </Grid>   
    </Expander>

    <Expander Grid.Row="2" IsExpanded="True" Header="First Data Entry Records">

        <DataGrid x:Name="FirstEntries"
            AutoGenerateColumns="False"
            BaseControls:DataGridExtension.Columns="{Binding FirstEntryGridColumns}"
            CanUserAddRows="False" IsReadOnly="True"
            SelectedItem="{Binding Path=SelectedFirstEntry}">
...
        </DataGrid>

    </Expander>

    <Expander Grid.Row="3" IsExpanded="True" Header="Enrollment Files Upload">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.ColumnSpan="3" Margin="3,10">
 ...
            </StackPanel>

            <TextBlock x:Name="SelectedFirstEntry_NameOnAccount" Grid.Row="1" Grid.Column="0"  />
            <TextBlock x:Name="SelectedFirstEntry_AccountNo" Grid.Row="1" Grid.Column="1" Margin="3,0" />
            <TextBlock x:Name="SelectedFirstEntry_MeterNo" Grid.Row="1" Grid.Column="2" Margin="3,0"/>

            <DataGrid x:Name="Findings" Grid.Row="2" 
                 AutoGenerateColumns="False"
                 BaseControls:DataGridExtension.Columns="{Binding FindingsGridColumns}"
                 CanUserAddRows="False">
 ...
            </DataGrid>
        </Grid>
    </Expander>
</Grid>

Thanks

Upvotes: 1

Views: 319

Answers (2)

bic
bic

Reputation: 2281

The expander control is a wierd one. There are some things you need to avoid regarding space used etc. Have a look at the documentation

Specifically,

"The border shows even when the content is collapsed. To set the size of the expanded content area, set size dimensions on the content of the Expander, or if you want scrolling capability, on the ScrollViewer that encloses the content."

So it looks like you'll have to rethink your UI and used fixed content sizes with scrollviewers or use a TabControl with 3 tabs for example?

Upvotes: 0

franssu
franssu

Reputation: 2430

The expanders do collapse but you can't see it because your row definition have constant heights, try :

<RowDefinition Height="auto"></RowDefinition>

(on the top grid)

Upvotes: 2

Related Questions