Reputation: 2280
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:
How can I do this? Thanks in advance.
Upvotes: 0
Views: 245
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
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