Barney Chambers
Barney Chambers

Reputation: 2783

XAML - Collapse Grid when hidden

I am creating an app in Xamarin Forms. I have a <Grid / > with an <Image/> inside it. The image is 200px high, even when no image is stored inside it. How do I collapse this grid to 0px, when no image is stored in the <Image/> tag?

<Grid x:Name="createGrid">
  <Grid.RowDefinitions>
    <RowDefinition Height="200"/>
  </Grid.RowDefinitions>
  <Image x:Name="Main Image" Grid.Row="0"/>
</Grid>

Upvotes: 0

Views: 1536

Answers (1)

VenkyDhana
VenkyDhana

Reputation: 905

Create a View Model like below

    public class ViewModel
    {
        ImageSource imagePath;
        public ImageSource ImagePath
        {
           get { return imagePath;}
           set
            {
                imagePath = value;
                if (imagePath != null)
                {
                    GridSize = 100;
                }
                else
                {
                    GridSize = 0;
                }

            }
        }

        public int GridSize { get; set; }

    }

Set this Model as Binding context in the page

ViewModel _MyModel = new ViewModel() {  };
BindingContext = _MyModel;
InitializeComponent();

Bind the Model in Xaml page

<Grid HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="{Binding GridSize}" WidthRequest="{Binding GridSize}" >
   <Image Source="{Binding ImagePath}"/>
</Grid>

Upvotes: 3

Related Questions