title: ScrollIntoView - item should not disapear from view
body: <p>From the start I wanna say I will award a 200 Bounty to somebody who can help me with my problem.</p>
  
  <p>This is my simple code(C# with WPF):</p>
  
  <pre><code>namespace WpfApplication1
  {
      /// &lt;summary&gt;
      /// Interaction logic for Window1.xaml
      /// &lt;/summary&gt;
      /// 
  
      public partial class Window1 : Window
      {
          string fixedItem;
  
          public Window1()
          {
              InitializeComponent();
              listBox1.ItemContainerGenerator.ItemsChanged += new System.Windows.Controls.Primitives.ItemsChangedEventHandler(list_changes);
  
          }
  
  
  
          private void list_changes(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs e)
          {
              listBox1.ScrollIntoView(fixedItem);
          }
  
          private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {      
              fixedItem = (string)listBox1.SelectedItem;        
          }
  
          private void button1_Click(object sender, RoutedEventArgs e)
          {
              listBox1.Items.Add("item0");
              listBox1.Items.Add("item1");
              listBox1.Items.Add("item2");
              listBox1.Items.Add("item3");
              listBox1.Items.Add("item4");
              listBox1.Items.Add("item5");
              listBox1.Items.Add("item6");
  
          }
  
          private void button2_Click(object sender, RoutedEventArgs e)
          {
              listBox1.Items.Insert(0, "item7");
              listBox1.Items.Insert(0, "item8");
              listBox1.Items.Insert(0, "item9");
              listBox1.Items.Insert(0, "item10");
              listBox1.Items.Insert(0, "item11");
              listBox1.Items.Insert(0, "item12");
              listBox1.Items.Insert(0, "item13");
              listBox1.Items.Insert(0, "item14");
              listBox1.Items.Insert(0, "item15");
          }
  
          private void button3_Click(object sender, RoutedEventArgs e)
          {
              listBox1.Items.Insert(0, "item16");
              listBox1.Items.Insert(0, "item17");
              listBox1.Items.Insert(0, "item18");
              listBox1.Items.Insert(0, "item19");
              listBox1.Items.Insert(0, "item20");
              listBox1.Items.Insert(0, "item21");
              listBox1.Items.Insert(0, "item22");
              listBox1.Items.Insert(0, "item23");
              listBox1.Items.Insert(0, "item24");
          }
      }
  }
  </code></pre>
  
  <p>First I just made 3 Buttons to insert some text in the listBox. Lets say I click button1 and button2, I will have this list:</p>
  
  <pre><code>item15
  item14
  item13
  item12
  ......
  item7
  item0
  item1
  .....
  item6
  </code></pre>
  
  <p>After that I want to click on "item12", then when I click button3 I want my "item12" to remain in the same spot while the text is generated(4th position in the list).<br>
  In short words everytime I click on item I want it to remain in the exctly same position while generating text. </p>
  
  <p>So anyone has any idee how to do this ? Do I need to use ScrollViewer object to work with VerticallOffset and ViewportHeigth ? This simple code that I posted when I click on the item and then generate text it will move the item at the bottom(viewable position) and remains there after. But I don't want to move it at all.</p>
  
  <p>Edit:</p>
  
  <p>Ok I tried this code from <a href="http://blogs.msdn.com/b/delay/archive/2009/04/19/fewer-gotchas-to-getcha-enhancing-the-scrollintoviewcentered-method-for-wpf-s-listbox.aspx" rel="nofollow">here</a>:</p>
  
  <pre><code>FrameworkElement container = listRadioItems.ItemContainerGenerator.ContainerFromItem(fixedItem) as FrameworkElement;
  
              if (null != container)
              {
                  if (ScrollViewer.GetCanContentScroll(listBox))
                  {
                      IScrollInfo scrollInfo = VisualTreeHelper.GetParent(container) as IScrollInfo;
                      if (null != scrollInfo)
                      {
                          StackPanel stackPanel = scrollInfo as StackPanel;
                          VirtualizingStackPanel virtualizingStackpanel = scrollInfo as VirtualizingStackPanel;
  
                          int index = listBox.ItemContainerGenerator.IndexFromContainer(container);
  
                          if (((null != stackPanel) &amp;&amp; (Orientation.Horizontal == stackPanel.Orientation)) || ((null != virtualizingStackpanel) &amp;&amp; (Orientation.Horizontal == virtualizingStackpanel.Orientation)))
                          {
                              scrollInfo.SetHorizontalOffset(index - Math.Floor(scrollInfo.ViewportWidth / 2));
                          }
                          else
                          {
                              scrollInfo.SetVerticalOffset(index - Math.Floor(scrollInfo.ViewportHeight / 2));
                          }
                      }
                  }
                  else
                  {
                      Rect rect = new Rect(new Point(), container.RenderSize);
  
                      FrameworkElement constrainingParent = container;
                      do
                      {
                          constrainingParent = VisualTreeHelper.GetParent(constrainingParent) as FrameworkElement;
                      } while ((null != constrainingParent) &amp;&amp; (listBox != constrainingParent) &amp;&amp; !(constrainingParent is ScrollContentPresenter));
  
                      if (null != constrainingParent)
                      {
                          rect.Inflate(Math.Max((constrainingParent.ActualWidth - rect.Width) / 2, 0), Math.Max((constrainingParent.ActualHeight - rect.Height) / 2, 0));
                      }
  
                      container.BringIntoView(rect);
                  }
              }
  </code></pre>
  
  <p>What it does for me is it centeres the item selected, but the scroll goes down, only sometimes it also centeres. And my problem is that again, only sometimes the selected item will disapear from view.</p>
  
  <p>What would be fantastic is if I can make both the item and the scroll centered. But first concern is the item should not disapear from view.</p>