Manoher Kumar
Manoher Kumar

Reputation: 281

Hidden Stacklayout taking up space in xamarin forms

I'm creating a form in Xaml using Xamrin which contains absolute layout in which i'm hiding a stacklayout. but one more stacklayout just down below of hidden stacklayout but hidden stacklayout is taking up space.

What i want to do.When i did hide one stacklayout another stacklayout should take place of hidden staklayout.

Upvotes: 7

Views: 11071

Answers (4)

Manoher Kumar
Manoher Kumar

Reputation: 281

Thanks for help and supports.

I've resolved the issue:

I followed this link https://forums.xamarin.com/discussion/83632/hiding-and-showing-stacklayout in this link they said that use grid and make row height auto and it will automatically adjust the extra space of layout.

<Grid VerticalOptions="Fill">    
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <StackLayout Grid.Row="1" Spacing="20">
        <StackLayout Margin="10,0">  
            <Label Text="lable 1" VerticalOptions="Center" FontSize="Small" />
            <Label Text="lable 2" VerticalOptions="Center" FontSize="Small" />
        </StackLayout>
        <StackLayout IsVisible="{Binding IsStudent}" Margin="10,0">  
            <Label Text="lable3" VerticalOptions="Center" FontSize="Small" />
            <Label Text="lable 4" VerticalOptions="Center" FontSize="Small" />
            <Label Text="lable 5" VerticalOptions="Center" FontSize="Small" />
            <Label Text="lable 6" VerticalOptions="Center" FontSize="Small" />
        </StackLayout>
    </StackLayout>
    <StackLayout Grid.Row="2" Spacing="20" >
        <local:Button
            x:Name="btnSave"
            Text="Submit"
            VerticalOptions="End"
            HorizontalOptions="FillAndExpand"
            IsVisible="{Binding IsBusy, Converter={x:Static local:InverseBoolConverter.Instance}}"
            AutomationId="saveButton" /> 
    </StackLayout>
</Grid>

Upvotes: 7

phil soady
phil soady

Reputation: 11328

No need for a grid. Set the IsVisible AND HeightRequest properties.

 MyStackLayout.IsVisible = false;
 MyStackLayout.HeightRequest = 0; // trigger recalc of space layout. 

The change in heightrequest triggers the desired recalculations.

Upvotes: 5

Sergey Ivanov
Sergey Ivanov

Reputation: 137

I've too looked into why those hidden controls are taking space and well... they ARE taking space and that's it. You can do a simple addition and removing of labels. Something like this:

public class MyStack : StackLayout
{
    Label
        _label1 = new Label(),
        _label2 = new Label(),
        _label3 = new Label();

    public void ShowLabels()
    {
        Children.Add(_label1);
        Children.Add(_label2);
        Children.Add(_label3);
    }

    public void HideLabels()
    {
        Children.Remove(_label1);
        Children.Remove(_label2);
        Children.Remove(_label3);
    }
}

Upvotes: 1

Joel Hernandez
Joel Hernandez

Reputation: 2213

My recomendation is putting a StackLayout at the bottom of the content, in order to keep the original height of the other elements at the top.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CentroDono.Views.YOURVIEWPAGE"
              Title="YOURVIEWTITLE"
             x:Name="YOURPAGECONTENTNAME">
    <ContentPage.Content>
        <StackLayout>
        <!--VISIBLE CONTENT-->
            <Label Text="HELLO"/>
        </StackLayout>
        <StackLayout>
            <Label Text="FOOTER"/>
            <!--INVISIBLE CONTENT-->    
            <Label x:Name="_searchText"  Text="Result1" IsVisible="False"/>
            <Label x:Name="_searchText2"  Text="Result2" IsVisible="False"/>
            <Label x:Name="_searchText3"  Text="Result3" IsVisible="False"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Upvotes: 0

Related Questions