Aquí hay una solución de hackeo. Podemos usar Value Conversion con DataBinding. Así que el primer paso es declarar nuestra ValueConvertor:
public class ListItemToPositionConverter : IValueConverter
{
#region Implementation of IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var item = value as ListBoxItem;
if (item != null)
{
var lb = FindAncestor<ListBox>(item);
if (lb != null)
{
var index = lb.Items.IndexOf(item.Content);
return index;
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Declarar donde desea que este método estático con el fin de obtener ListBox padres:
public static T FindAncestor<T>(DependencyObject from) where T : class
{
if (from == null)
return null;
var candidate = from as T;
return candidate ?? FindAncestor<T>(VisualTreeHelper.GetParent(from));
}
Luego, en ListBox.Resources declarar nuestro convertidor de la siguiente manera:
<ListBox.Resources>
<YourNamespace:ListItemToPositionConverter x:Key="listItemToPositionConverter"/>
</ListBox.Resources>
Y, por último - DataTemplate:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Converter={StaticResource listItemToPositionConverter}}"/>
<Label Content="{Binding Path=DisplayName}"></Label>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Nota: En este ejemplo, los elementos se numerarán comenzando por 0 (cero), puede cambiarlo en el método Convertir agregando 1 al resultado.
Espero que esto ayude ...