Given the following XAML code that with a ListControl like behavior:
<StackPanel>
<ItemsControl Name="_listbox" ItemsSource="{Binding ElementName=_userControl, Path=DataContext}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel>
...
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
Since the list can be long (100-200 items), and the items look similar, I think it would helpful for the user during scrolling if every item would display their position in the list. How could an item in the template know its own position in the list?
Here is a hack solution. We can use Value Conversion with DataBinding. So the first step is to declare our ValueConvertor:
Declare wherever you want this static method in order to obtain ListBox parent:
Then in ListBox.Resources declare our convertor as follows:
And finally – DataTemplate:
Note: in this example the items will be numerated starting with 0 (zero), you can change it in Convert method by adding 1 to result.
Hope this helps…