Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 661
Manoher Kumar Avatar asked Aug 11 '17 10:08

Manoher Kumar


3 Answers

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>
like image 109
Manoher Kumar Avatar answered Oct 21 '22 04:10

Manoher Kumar


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.

like image 21
phil soady Avatar answered Oct 21 '22 02:10

phil soady


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);
    }
}
like image 43
Sergey Ivanov Avatar answered Oct 21 '22 03:10

Sergey Ivanov