title: WPF - ScrollViewer will not scroll when ScrollToHorizontalOffset() is called
body: <p>I have a Scrollviewer inside a TabControl, and for some reason when you change tabs my ScrollViewer jumps to HorizontalOffset of 12.  My ScrollViewer, among other things has a ListView inside it with an unrestricted height.  It's a strange layout, but everything inside the scroller had to be printable so I couldn't easily allow the ListView to scroll on its own.</p>
  
  <p>So I tried the following lines in the SelectionChanged event of the TabControl:</p>
  
  <pre><code>Console.WriteLine(Scroller.HorizontalOffset);
  Scroller.ScrollToHorizontalOffset(0);
  Console.WriteLine(Scroller.HorizontalOffset);
  </code></pre>
  
  <p>And they print out 12 for both calls to WriteLine()...  It also does not move, but you can scroll from the actual UI.</p>
  
  <p>Does anyone know what would cause this strange behavior?</p>
  
  <p>I will try to find suitable code to post, but it's a large project and might be difficult to recreate in a sample solution.</p>
  
  <p>Other info:</p>
  
  <ul>
  <li>The issue only happens if the content is large enough where it can horizontally scroll.</li>
  <li>My horizontal and vertical scrollbar visibility is Auto, but the issue still happens if they are set to Hidden or Visible.</li>
  </ul>
  
  <p>UPDATE:</p>
  
  <p>If you click my ListView that's inside the ScrollViewer and select an item, the ScrollViewer scrolls to this same 12 pixel offset.  Could my issue be related to my ListView getting focus?  Does a ListView call BringIntoView() on itself when it receives focus?</p>
  
  <p>This Xaml reproduces the issue:</p>
  
  <pre><code>&lt;Window 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window11" Height="300" Width="300"&gt;
  &lt;TabControl&gt;
      &lt;TabItem Header="Tab One"&gt;
          &lt;ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"&gt;
              &lt;ListBox Margin="25,0,0,0" Width="2000"&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;          
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;/ListBox&gt;
          &lt;/ScrollViewer&gt;
      &lt;/TabItem&gt;
      &lt;TabItem Header="Tab Two"&gt;
          &lt;ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"&gt;
              &lt;ListBox Margin="25,0,0,0" Width="2000"&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;          
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;ListBoxItem&gt;WOOT WOOT WOOT WOOT&lt;/ListBoxItem&gt;
              &lt;/ListBox&gt;
          &lt;/ScrollViewer&gt;
      &lt;/TabItem&gt;
  &lt;/TabControl&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Slap this in XamlPad, run it.  Click a Tab header and notice the ScrollViewer scroll on its own, play around with it and notice how my margin on the ListBox is scrolled off screen.  How do I prevent or workaround this behavior?  It makes my layout look pretty janky in my case.</p>