title: How to bind and display ListBoxItem index in ListBox?
body: <p>I have a .NET 5 project with following Nuget Packages:</p>
  <ul>
  <li>HandyControls for UI</li>
  <li>Gong-Wpf-DragDrop for Drag and Drop elements in a List</li>
  </ul>
  <p>I have a <code>XAML with a </code>ListBox<code>and a ViewModel with a</code>ObservableCollection` of Model.</p>
  <p>The <code>ObservableCollection</code> is binded as <code>ItemSource</code> of <code>ListBox</code></p>
  <p><strong>What I want to achieve:</strong></p>
  <p>When i Drag and Drop an item in a different position (or Add/Delete), I want the indexes to be refreshed.</p>
  <p>Example:</p>
  <p><em>Before Drag/Drop</em></p>
  <p><a href="https://i.stack.imgur.com/FbDTs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FbDTs.png" alt="enter image description here" /></a></p>
  <p><em>After Drag/Drop</em></p>
  <p><a href="https://i.stack.imgur.com/pZSco.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pZSco.png" alt="enter image description here" /></a></p>
  <p>Actually, i binded the drophandler of gong-wpf-dragdrop
  and at the end of the drop, i manually refresh every single Index in my list.</p>
  <p>there is a way to do it easily? because actually i have to refresh indexes manually.</p>
  <p><em>Summarizing:</em>
  When i reorder/delete/add items i want Model.Index of every item updated with the correct index position in ListBox.</p>
  <p><strong>My Mandate is:</strong></p>
  <ul>
  <li>Show index (one based)</li>
  <li>Give the possibility to reorder the elements</li>
  </ul>
  <p>I tried looking for similar questions but didn't find much that could help me.
  Thanks in advance :)</p>
  <hr />
  <p><strong>Model:</strong></p>
  <pre><code>public class Model : BindablePropertyBase
  {
      private int index;
      private string name;
  
      public int Index
      {
          get { return index; }
          set
          {
              index = value;
              RaisePropertyChanged();
          }
      }
      public string Name
      {
          get { return name; }
          set
          {
              name = value;
              RaisePropertyChanged();
          }
      }
  }
  </code></pre>
  <p>And below a xaml with a simple binded list
  <strong>MainWindow.xaml</strong></p>
  <pre><code>&lt;hc:Window x:Class=&quot;ListBoxIndexTest.MainWindow&quot;
          xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
          xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
          xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
          xmlns:hc=&quot;https://handyorg.github.io/handycontrol&quot;
          xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
         xmlns:dd=&quot;clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop&quot;
      xmlns:local=&quot;clr-namespace:ListBoxIndexTest&quot;
      mc:Ignorable=&quot;d&quot;
      Title=&quot;MainWindow&quot; Height=&quot;450&quot; Width=&quot;600&quot;&gt;
  &lt;Window.DataContext&gt;
      &lt;local:MainWindowViewModel /&gt;
  &lt;/Window.DataContext&gt;
  &lt;Grid&gt;
      &lt;ListBox ItemsSource=&quot;{Binding TestList}&quot; dd:DragDrop.DropHandler=&quot;{Binding}&quot;
               dd:DragDrop.IsDropTarget=&quot;True&quot; dd:DragDrop.IsDragSource=&quot;True&quot;&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid Margin=&quot;10&quot;
                        Background=&quot;Aqua&quot;&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                          &lt;ColumnDefinition /&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
  
                      &lt;TextBlock Text=&quot;{Binding Index}&quot; Margin=&quot;10,0&quot;/&gt;
  
                      &lt;TextBlock Text=&quot;{Binding Name}&quot; 
                                 Grid.Column=&quot;1&quot;/&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  <p>&lt;/hc:Window&gt;</p>
  <p><strong>MainWindowViewModel.cs</strong></p>
  <pre><code>public class MainWindowViewModel : BindablePropertyBase, IDropTarget
  {
      private ObservableCollection&lt;Model&gt; test_list;
      public ObservableCollection&lt;Model&gt; TestList
      {
          get 
          {
              return test_list;
          }
          set
          {
              test_list = value;
              RaisePropertyChanged();
          }
      }
  
      // Constructor
      public MainWindowViewModel()
      {
          TestList = new ObservableCollection&lt;Model&gt;()
          {
              new Model()
              {
                  Index = 1,
                  Name = &quot;FirstModel&quot;
              },
              new Model()
              {
                  Index = 2,
                  Name = &quot;SecondModel&quot;
              },
              new Model()
              {
                  Index = 3,
                  Name = &quot;ThirdModel&quot;
              }
          };
      }
  
      public void DragOver(IDropInfo dropInfo)
      {
          Model sourceItem = dropInfo.Data as Model;
          Model targetItem = dropInfo.TargetItem as Model;
  
          if (sourceItem != null &amp;&amp; targetItem != null)
          {
              dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
              dropInfo.Effects = DragDropEffects.Move;
          }
      }
  
      public void Drop(IDropInfo dropInfo)
      {
          Model sourceItem = dropInfo.Data as Model;
          Model targetItem = dropInfo.TargetItem as Model;
  
          if(sourceItem != null &amp;&amp; targetItem != null)
          {
              int s_index = sourceItem.Index - 1;
              int t_index = targetItem.Index - 1;
  
              TestList.RemoveAt(s_index);
              TestList.Insert(t_index, sourceItem);
  
              RefreshAllIndexes();
          }
      }
  
      private void RefreshAllIndexes()
      {
          for (int i = 0; i &lt; TestList.Count; i++)
          {
              TestList[i].Index = i + 1;
          }
      }
  }
  </code></pre>