halileohalilei
halileohalilei

Reputation: 2280

How to bind data together with the item index on Windows Phone 8

I have an ItemsControl in my ApplicationPage.xaml and I'm binding data to it with the following code:

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Margin="24" Text="{Binding someData}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I want it to also display the item index next to someData, such that if the bound strings are {"First string", "Second string", "Third string", ...}, it would look like:

  1. First string
  2. Second string
  3. Third string

How can I do this? Thanks in advance.

Upvotes: 0

Views: 245

Answers (2)

Onur Tırpan
Onur Tırpan

Reputation: 537

string[] someData = { "First string", "Second string", "Third string" };

for (int i = 0; i < someData.Length; i++)
{ someData[i] = i.ToString() + someData[i]; }

This could do what you want, but if you want to add these number like an another binding object, You should create a class and it should contains "number" and "content" variables. And then you can bind them.

If first solution won't enough for you, I can help you about creating classes and binding them.

Upvotes: 1

Jaihind
Jaihind

Reputation: 2778

May this will help you String.Format Method Those are alignment and formatString in index [,alignment] [:formatString].

<TextBlock Text={Binding SomeDate,StringFormat='{}stuff happened on {0,10:d}'} />

Upvotes: 0

Related Questions