title: Set and Scroll to SelectedIndex of ListBox in Pivot in WP10
body: <p>I am trying to bind <code>SelectedIndex</code> of <code>ListBox</code> inside Pivot. Headers, Items are bind correctly, however, the <code>ListBox</code> <code>SelectedIndex</code> is somehow not working.</p>
  
  <p>XAML</p>
  
  <pre><code>&lt;Page.DataContext&gt;
          &lt;local:ChapterMenuViewModel/&gt;
      &lt;/Page.DataContext&gt;
  &lt;Pivot x:Name="pvMain" TitleTemplate="{StaticResource PivotTitleTemplate}"
    HeaderTemplate="{StaticResource PivotHeaderTemplate}"
    ItemsSource="{Binding ChapterMenuHeader}" SelectionChanged="pvMain_SelectionChanged"&gt;
    &lt;Pivot.ItemTemplate&gt;
      &lt;DataTemplate&gt;
        &lt;Grid Grid.Row="1" Grid.Column="0"&gt;
        &lt;ListBox FlowDirection="RightToLeft" FontFamily="./Fonts/ScheherazadeRegOT.ttf#Scheherazade" 
         x:Name="lsbChapter" ItemTemplate="{StaticResource ChapterItemTemplate}"
         SelectedIndex="{Binding SelectedChapterIndex}"
         ItemsSource="{Binding Chapters}"&gt;
        &lt;/ListBox&gt;
      &lt;/Grid&gt;
    &lt;/DataTemplate&gt;
   &lt;/Pivot.ItemTemplate&gt;
  &lt;/Pivot&gt;
  </code></pre>
  
  <p>MVVM</p>
  
  <pre><code>public class ChapterMenuViewModel : INotifyPropertyChanged
      {
          ObservableCollection&lt;ChapterMenusHeader&gt; _chapterMenuHeader;
          DataSource ds = null;
          public ChapterMenuViewModel()
          {
              ChapterMenuHeader = new ObservableCollection&lt;ChapterMenusHeader&gt;();
              ds = new DataSource();
  
              List&lt;JuzDetail&gt; allJuz = DataSource.GetAllJuz;
              ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "chapter", Chapters = DataSource.GetAllChapter, Juzs = allJuz });
              ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "location", Chapters = DataSource.GetAllChapterSortedByChapterType, Juzs = allJuz });
              ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "order", Chapters = DataSource.GetAllChapterSortedByOrder, Juzs = allJuz });
              ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "size", Chapters = DataSource.GetAllChapterSortedBySize, Juzs = allJuz });
              ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "arabic name", Chapters = DataSource.GetAllChapterSortedByArabicAlphabet, Juzs = allJuz });
              ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "english name", Chapters = DataSource.GetAllChapterSortedByEnglishAlphabet, Juzs = allJuz });
          }
          public ObservableCollection&lt;ChapterMenusHeader&gt; ChapterMenuHeader
          {
              get { return _chapterMenuHeader; }
              set
              {
                  if (_chapterMenuHeader != value)
                  {
                      _chapterMenuHeader = value;
                      OnPropertyChanged("ChapterMenuHeader");
                  }
              }
          }
          public event PropertyChangedEventHandler PropertyChanged;
          public void OnPropertyChanged(string propertyName)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
              }
          }
          public class ChapterMenusHeader : INotifyPropertyChanged
          {
              public event PropertyChangedEventHandler PropertyChanged;
              public void OnPropertyChanged(string propertyName)
              {
                  if (PropertyChanged != null)
                  {
                      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                  }
              }
  
              public ChapterMenusHeader()
              {
                  SelectedChapterIndex = App.Recent.ChapterID;
              }
              string _header;
              public string Header
              {
                  get { return _header; }
                  set
                  {
                      if (_header != value)
                      {
                          _header = value;
                          OnPropertyChanged("Header");
                      }
                  }
              }
  
              List&lt;Chapter&gt; _allChapters;
              public List&lt;Chapter&gt; Chapters
              {
                  get { return _allChapters; }
                  set
                  {
                      if (_allChapters != value)
                      {
                          _allChapters = value;
                          OnPropertyChanged("Chapters");
                      }
                  }
              }
  
              int _selectedChapterIndex;
              public int SelectedChapterIndex
              {
                  get { return _selectedChapterIndex; }
                  set
                  {
                      _selectedChapterIndex = value;
                      OnPropertyChanged("SelectedChapterIndex");
                  }
              }
              List&lt;JuzDetail&gt; allJuz;
              public List&lt;JuzDetail&gt; Juzs
              {
                  get { return allJuz; }
                  set
                  {
                      if (allJuz != value)
                      {
                          allJuz = value;
                          OnPropertyChanged("Juzs");
                      }
                  }
              }
          }
  </code></pre>
  
  <p>Scrolling Section</p>
  
  <pre><code>private void lsbChapter_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
     ListBox lsb = sender as ListBox;
     if (lsb.SelectedIndex != -1)
        scrollIntoSelectedItem(lsb, lsb.SelectedIndex);
   }
  
  void scrollIntoSelectedItem(ListBox lsb, int index)
  {
     lsb.SelectionChanged -= lsbChapter_SelectionChanged;
     lsb.SelectedIndex = lsb.Items.Count - 1;
     lsb.UpdateLayout();
     lsb.ScrollIntoView(lsb.SelectedIndex);
  
     lsb.SelectedIndex = index;
     lsb.UpdateLayout();
     lsb.ScrollIntoView(index);
  
     lsb.SelectionChanged += lsbChapter_SelectionChanged;
  }
  </code></pre>
  
  <p>This is only portion of the <code>ViewModel</code> class where I am binding the <code>SelectedIndex</code> of <code>ListBox</code>.
  The <code>ListBox</code> items are bind correctly, however, the <code>SelectedIndex</code> is not working. 
  How to set and scroll the <code>SelectedIndex</code> of <code>ListBox</code> in Pivot?</p>
  
  <p>Thanks!</p>