Search code examples
xamarin.formsdata-binding

how to pass multiple labels through ContentPage


I am looking for help setting the code behind to pass multiple Labels using the next button. Basically I want to have a label set when the page is opened, press the next button and have a new label replace the current one (without setting new content pages). I a beginner working in Xamarin.Forms and I am not really understanding the data binding process... If anyone has a good reference (other than the Microsoft Website) that would help as well. Pretty sure the code below will not do anything yet... Thanks in advance :)

this is the ContentPage:

<ContentPage.Content>
    <StackLayout>
        <Label Text="{Binding TitleText}" />
        <ScrollView VerticalOptions="FillAndExpand">
            <StackLayout>
                <Label Text="{Binding EngText}" />

                <Label Text="{Binding ItText}" />

            </StackLayout>
        </ScrollView>

This is what I started for the code behind:

''''''

namespace MVVM2
{
public partial class MainPage : ContentPage
{
    List<MainPage> Contacts { get; set; }
    int ndx = 0;

    public string TitleText { get; set; }
    public string EngText { get; set; }
    public string ItText { get; set; }

    public MainPage()
    {
        InitializeComponent();

        Contacts = new List<MainPage>();

        // repeat this for as many contacts as you need
        Contacts.Add(new MainPage
        {
            TitleText = "Title1",
            EngText = "EngText1",
            ItText = "ItText1"
        });

        Contacts.Add(new MainPage
        {
            TitleText = "Title2",
            EngText = "EngText2",
            ItText = "ItText2"
        });

        Contacts.Add(new MainPage
        {
            TitleText = "Title3",
            EngText = "EngText3",
            ItText = "ItText3"
        });

        // display the first contact
        BindingContext = Contacts[ndx];
    }

    private void OnNavigateButtonClicked(object sender, EventArgs e)
    {
        // increment your index
        ndx++;

        // check that we haven't gone too far
        if (ndx < Contacts.Count)
        {
            BindingContext = Contacts[ndx]; 
        }
    }
  }
}

Solution

  • if you just want to display different text when the button is clicked, you don't need to navigate to a new page

    first, create a List to hold your buttons and a variable to keep track of which one is displayed. These two lines should be in the body of your class but NOT inside any specific method

    List<Contact> contacts { get; set; }
    int ndx = 0;
    

    then in your constructor setup your data

    public MainPage()
    {
        InitializeComponent();
     
        contacts = new List<Contact>();
    
        // repeat this for as many contacts as you need
        contacts.Add(new Contact { 
            TitleText = "Title1",
            EngText = "EngText1",
            ItText = "ItText1"});
       
        // display the first contact
        BindingContext = contacts[ndx];
    }
    

    finally, handle the button click

    async void OnNavigateButtonClicked(object sender, EventArgs e)
    {
       // increment your index
       ndx++;
    
       // check that we haven't gone too far
       if (ndx < contacts.Count) {
         BindingContext = contacts[ndx];
       }
    }