tags:

views:

1015

answers:

2

Hi,

I would like my listbox to number each item using the SelectedIndex property + 1.

How would I do that to the Text property of a TextBlock in a DataTemplate of the listbox???

Malcolm

EDIT: i dont think I mean the Selectedindex just the index of the listboxitem.

A: 

If each ListBoxItem uses SelectedIndex + 1, they will all display the same value since SelectedIndex is a scalar. Moreover, this number will change as the user selects different ListBoxItems. I suspect you actually want to display each item's index within the ListBox + 1.

To achieve this, you're probably best off using the ListBox's ItemsContainerGenerator to get the index of the item within the container (see the IndexFromContainer method). You could look at exposing this from your data class, or perhaps look into an attached readonly property that retrieves this value for you.

HTH, Kent

Kent Boogaart
A: 

I had the same question. So far I'm just using my data model to provide the numbers...

Sergey Aldoukhov