Search code examples
c#xamlxamarintabbedpage

How to access StackLayout from the backend?


I have been using Xamarin Forms to develop iOS and Android applications. I want to access a StackLayout that is within a TabbedPage so I can make it visible or hidden whenever the user changes tabs, but when I try to access the StackLayout I get "This does not exist in the current context". Here is my XAML code and my CS code.

CS

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace DebuggerTestAndroidIOS
{
    public partial class PatientTabPage : TabbedPage
    {
        public PatientTabPage ()
        {
            InitializeComponent ();
            ItemsSource = PatientDataModel.tabs;
            //vitalSignsStack.IsVisible = true;

            this.CurrentPageChanged += (object sender, EventArgs e) => {
                var i = this.Children.IndexOf(this.CurrentPage);
                System.Diagnostics.Debug.WriteLine("Page No:"+i);

                if (i == 1){
                    vitalSignsStack.IsVisible = true;
                }           
            };
        }
    }
}

XAML

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="DebuggerTestAndroidIOS.PatientTabPage">
    <TabbedPage.ItemTemplate>
        <DataTemplate>
            <ContentPage Title ="{Binding TabName}">
                <!--Parent Wrapper layout-->
                <StackLayout Orientation="Vertical" BackgroundColor="White">
                   <StackLayout x:Name="vitalSignsStack" Orientation="Horizontal" IsVisible="false">
                        <Image Source="VitalSigns.png" HorizontalOptions="Center"/>
                    </StackLayout>
                </StackLayout><!--End parent wrapper-->
            </ContentPage>
        </DataTemplate>
    </TabbedPage.ItemTemplate>
</TabbedPage>

Solution

  • Thanks for your efforts. I tried them, still was not able to access the StackLayouts. I modified a little bit my code, this helped a lot and made everything easier: Creating different layouts for each tab