title: Numbered listbox
body: <p>I have a sorted listbox and need to display each item's row number. In this demo I have a Person class with a Name string property. The listbox displays a a list of Persons sorted by Name. How can I add to the datatemplate of the listbox the row number???</p>
  
  <p>XAML:</p>
  
  <pre class="lang-xaml prettyprint-override"><code>&lt;Window x:Class="NumberedListBox.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="300" Width="300"&gt;
      &lt;ListBox 
          ItemsSource="{Binding Path=PersonsListCollectionView}" 
          HorizontalContentAlignment="Stretch"&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;TextBlock Text="{Binding Path=Name}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>using System;
  using System.Collections.ObjectModel;
  using System.Windows.Data;
  using System.Windows;
  using System.ComponentModel;
  
  namespace NumberedListBox
  {
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
  
              Persons = new ObservableCollection&lt;Person&gt;();
              Persons.Add(new Person() { Name = "Sally"});
              Persons.Add(new Person() { Name = "Bob" });
              Persons.Add(new Person() { Name = "Joe" });
              Persons.Add(new Person() { Name = "Mary" });
  
              PersonsListCollectionView = new ListCollectionView(Persons);
              PersonsListCollectionView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
  
              DataContext = this;
          }
  
          public ObservableCollection&lt;Person&gt; Persons { get; private set; }
          public ListCollectionView PersonsListCollectionView { get; private set; }
      }
  
      public class Person
      {
          public string Name { get; set; }
      }
  }
  </code></pre>