james
james

Reputation: 181

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

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>

Upvotes: 15

Views: 20586

Answers (3)

Mochi
Mochi

Reputation: 1117

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 =)

Upvotes: 0

Harry
Harry

Reputation: 3115

To bind directly to the object you should use:

<Label Text="{Binding}" />

This is shorthand for:

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

Upvotes: 6

Jason
Jason

Reputation: 89204

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

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

Upvotes: 52

Related Questions