Shahid Od
Shahid Od

Reputation: 123

Binding to elements of a list in xamarin

So i've multiple Labels already created and i want to bind each label.text to an item of a list as the code shows. but it seems that i can't access the value of items by index

<StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack0" IsVisible="True">
                        <Label  x:Name="CountRep0" FontSize="18" TextColor="White" Text="{Binding Counter[0]}"/>

                        <Label  x:Name="Objname0" FontSize="18" TextColor="White" Text="{Binding Objets_de_Commande[0]}"/>

                    </StackLayout>

<StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack1" IsVisible="True">
                        <Label  x:Name="CountRep1" FontSize="18" TextColor="White" Text="{Binding Counter[1]}"/>

                        <Label  x:Name="Objname1" FontSize="18" TextColor="White" Text="{Binding Objets_de_Commande[1]}"/>

                    </StackLayout>

and I have 2 Lists "Objets_de_Commande" and "Counter" do you have any ideas how i can make this work ?

Upvotes: 0

Views: 2791

Answers (2)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13919

Of course, you can binding an item of a list by the index . for example:

MainPage.xaml.cs

 public partial class MainPage : ContentPage
{
    public List<string> Counter { get { return new List<string> { "1", "2" }; } }

    public List<string> Objets_de_Commande { get { return new List<string> { "test01", "test02" }; } }

    public MainPage()
    {
        InitializeComponent();

        BindingContext = this;
    }
}

MainPage.xaml

 <StackLayout Orientation="Vertical"  >
    <StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack0" IsVisible="True">
        <Label  x:Name="CountRep0" FontSize="18" TextColor="Black" Text="{Binding Counter[0]}"/>
        <Label  x:Name="Objname0" FontSize="18" TextColor="Black" Text="{Binding Objets_de_Commande[0]}"/>
    </StackLayout>
    <StackLayout Orientation="Horizontal" Margin="4,0,0,0" x:Name="stack1" IsVisible="True">
        <Label  x:Name="CountRep1" FontSize="18" TextColor="Black" Text="{Binding Counter[1]}"/>

        <Label  x:Name="Objname1" FontSize="18" TextColor="Black" Text="{Binding Objets_de_Commande[1]}"/>
    </StackLayout>
</StackLayout>

Note:

Just pay attention to the background and font color, if they are the same color,we could not see the result. So I change the color of text to Black:

TextColor="Black"

Upvotes: 1

pinedax
pinedax

Reputation: 9356

It's definitely possible.

You can access a list item using an index-based Binding using the same code you are typed:

<Label  x:Name="Objname0" FontSize="18" TextColor="White" Text="{Binding Objets_de_Commande[0]}"/>

Just make sure you are setting the right BindingContext to the page in code behind.

This other post shows how to do the binding in Code (C#) but it's not any different in XAML.

Note: As mentioned in one of the comments: it might be better for you to look into the ListView or the CollectionView instead.

Hope this helps.-

Upvotes: 2

Related Questions