14

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
  • 171
  • 1
  • 1
  • 10

3 Answers3

51

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

<Label Text="{Binding .}" />
Jason
  • 86,222
  • 15
  • 131
  • 146
  • I'm getting= Unexpected symbol `.', expecting `,', `;', or `=' With – james Mar 30 '17 at 19:48
  • 3
    @james What if you just try ``? – 15ee8f99-57ff-4f92-890c-b56153 Mar 30 '17 at 19:53
  • @EdPlunkett Works great thanks. Is this in the documentation by the way? I couldn't find it. – james Mar 30 '17 at 19:56
  • @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. – 15ee8f99-57ff-4f92-890c-b56153 Mar 30 '17 at 19:57
  • @EdPlunkett Do you know how I can use this with StringFormat? Text="{Binding, StringFormat=' {0:N}'}" this doesn't work – james Mar 30 '17 at 20:01
  • @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 Mar 30 '17 at 20:08
  • @james Wait, you're already binding to `Text`, and you're single-quoting the format string -- what I'm googling says that should work. – 15ee8f99-57ff-4f92-890c-b56153 Mar 30 '17 at 20:12
  • The error I get is "MarkupExtension not found for Binding," – james Mar 30 '17 at 20:23
  • 1
    Strangely enough, Text="{Binding ., StringFormat='Text = {0:N}'}" This works. You need the dot. Some decent documentation would be nice... – james Mar 30 '17 at 20:27
  • @EdPlunkett Haha I know right, it looks so random. Thanks for helping out bud – james Mar 30 '17 at 20:30
  • Confusion stems from mixing up pathing notation and object.property notation. They are typically equivalent but you cannot path into a List. You have to make the List 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. – Sean Anderson Nov 04 '18 at 17:13
  • What is the C#/Code-Behind version of this solution? – Mochi Jul 18 '19 at 18:21
5

To bind directly to the object you should use:

<Label Text="{Binding}" />

This is shorthand for:

<Label Text="{Binding Path=.}" />
Harry
  • 2,965
  • 1
  • 13
  • 19
0

In (MVVM/Code-Behind/C#), I had a similar problem with an array of strings and resolved it with the following code.

someLabel.SetBinding(Label.TextProperty, new Binding("."));

I hope this helps someone =)

Mochi
  • 1,059
  • 11
  • 26