How to bind the values of the itemsource (array of strings) to a label in a ListView

17,787

Solution 1

If you want to bind directly to the value of the object itself, use the "." syntax for the path

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

Solution 2

To bind directly to the object you should use:

<Label Text="{Binding}" />

This is shorthand for:

<Label Text="{Binding Path=.}" />
Share:
17,787
james
Author by

james

Updated on June 17, 2022

Comments

  • james
    james almost 2 years

    I have an array of strings, that I have set as the item source of a ListView. The ListView now has the same amount of rows as the array has elements. However I don't know what to set the binding as. I know for a Dictionary I set 'Value' which works fine.

    string[] array = {"1","2","3"};
    MyListView.ItemsSource = array;
    

    XAML

    <ListView x:Name="MyListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding Value, StringFormat='The value : {0:N}'}" />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    
  • james
    james about 7 years
    I'm getting= Unexpected symbol .', expecting ,', ;', or =' With <Label Text="{Binding .}" />
  • 15ee8f99-57ff-4f92-890c-b56153
    15ee8f99-57ff-4f92-890c-b56153 about 7 years
    @james What if you just try <Label Text="{Binding}" />?
  • james
    james about 7 years
    @EdPlunkett Works great thanks. Is this in the documentation by the way? I couldn't find it.
  • 15ee8f99-57ff-4f92-890c-b56153
    15ee8f99-57ff-4f92-890c-b56153 about 7 years
    @james No idea, I've never seen the Xamarin docs and wasn't sure what to google for. Just had a hunch that part might work the same as in WPF.
  • james
    james about 7 years
    @EdPlunkett Do you know how I can use this with StringFormat? Text="{Binding, StringFormat=' {0:N}'}" this doesn't work
  • 15ee8f99-57ff-4f92-890c-b56153
    15ee8f99-57ff-4f92-890c-b56153 about 7 years
    @james Oh, right -- StringFormat will be ignored, because the target property isn't of type String. Label.Content is Object. See if the Xamarin Label control has a ContentStringFormat property. If it doesn't, try using a TextBlock control instead of a Label.
  • 15ee8f99-57ff-4f92-890c-b56153
    15ee8f99-57ff-4f92-890c-b56153 about 7 years
    @james Wait, you're already binding to Text, and you're single-quoting the format string -- what I'm googling says that should work.
  • james
    james about 7 years
    The error I get is "MarkupExtension not found for Binding,"
  • james
    james about 7 years
    Strangely enough, Text="{Binding ., StringFormat='Text = {0:N}'}" This works. You need the dot. Some decent documentation would be nice...
  • james
    james about 7 years
    @EdPlunkett Haha I know right, it looks so random. Thanks for helping out bud
  • Sean Anderson
    Sean Anderson over 5 years
    Confusion stems from mixing up pathing notation and object.property notation. They are typically equivalent but you cannot path into a List<string>. You have to make the List<string> property the actual ItemSource and then use "Binding" or "Binding ." This scenario doesn't come up in all of examples and google searches--at the least it does not jump out at you.
  • Mochi
    Mochi almost 5 years
    What is the C#/Code-Behind version of this solution?