Two buttons, fill width of both if first is disabled

RezaMohamedRezaMohamed USMember ✭✭

I have two buttons arranged horizontally, if either buttons is disabled, I would like to take the full width. The following code works if both buttons IsEnabled="True", how would I take up the entire space if one is disabled? Not opposed to using a StackLayout either.

<Grid>
    <Grid.ColumnDefinations>
        <ColumnDefination Width="*"/>
        <ColumnDefination Width ="*"/>
    </Grid.ColumnDefinations>
    <Button Grid.Row ="0" Grid.Column="0" Text="B1" HorizontalOptions="FillAndExpand"/>
    <Button Grid.Row ="0" Grid.Column="1" Text="B2" HorizontalOptions="FillAndExpand"/>
</Grid>

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can try the following code:

        <StackLayout HorizontalOptions="FillAndExpand"  Orientation="Horizontal" >
                <Button Text="B1" HorizontalOptions="FillAndExpand" IsVisible="True"/>
                <Button Text="B2" HorizontalOptions="FillAndExpand" IsVisible="False"/>
            </StackLayout>
    

    Note: we should set property IsVisible="False" when we don't want the Button to show.

  • RezaMohamedRezaMohamed USMember ✭✭

    The FillAndExpand does not work with both the Grid and the StackLayout as I had mentioned in the question. For reference, this Grid/Stacklayout is inside another stacklayout...so not sure if that whats causing the fillandexpand to not work.

  • jezhjezh Member, Xamarin Team Xamurai

    Bases on my test, when I wraped StackLayout with another StackLayout,it just worked.
    When I tested with the following code:

      <StackLayout  Orientation="Vertical">
            <StackLayout HorizontalOptions="FillAndExpand"  Orientation="Horizontal" >
                <Button Text="B1" HorizontalOptions="FillAndExpand" IsVisible="False"/>
                <Button Text="B2" HorizontalOptions="FillAndExpand" IsVisible="True"/>
            </StackLayout>
        </StackLayout>
    

    The result is:

    And when I used code like this:

     <StackLayout  Orientation="Vertical">
        <StackLayout HorizontalOptions="FillAndExpand"  Orientation="Horizontal" >
            <Button Text="B1" HorizontalOptions="FillAndExpand" IsVisible="True"/>
            <Button Text="B2" HorizontalOptions="FillAndExpand" IsVisible="True"/>
        </StackLayout>
    </StackLayout>
    

    The result is:

    The whole xaml is:

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="FormApp1.MainPage">
        <StackLayout  Orientation="Vertical">
            <StackLayout HorizontalOptions="FillAndExpand"  Orientation="Horizontal" >
                <Button Text="B1" HorizontalOptions="FillAndExpand" IsVisible="True"/>
                <Button Text="B2" HorizontalOptions="FillAndExpand" IsVisible="True"/>
            </StackLayout>
        </StackLayout>
    </ContentPage>
    
  • ahmidiahmidi Member ✭✭

    yes we can try this code:




Sign In or Register to comment.