Is it possible to disable this built-in behavior of the default ScrollViewer in the WPF framework? The scenario is the following:
- I have an ScrollViewer with a StackPanel placed inside of it.
- In that StackPanel there many UIElement (for example TextBox controls)
- Focusing one of those TextBox controls (which is not currently visible) with the use of its Focus() method causes the TextBox to be brought in the view making it visible.
I want to disable this behavior but I am not sure how to approach this. Will I need to write my own ScrollViewer control or there is an attached property I am not aware of that disable this "ScrollIntoView" logic. Here is some code snippets of the scenario:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel>
<Button Content="Focus Item 6" Margin="10" Click="Button_Click"/>
<TextBox Text="Lost focus" Margin="10"/>
</StackPanel>
<ScrollViewer Grid.Row="1">
<StackPanel>
<TextBox Text="Item 1" Height="100" x:Name="Item1"/>
<TextBox Text="Item 2" Height="100" x:Name="Item2"/>
<TextBox Text="Item 3" Height="100" x:Name="Item3"/>
<TextBox Text="Item 4" Height="100" x:Name="Item4"/>
<TextBox Text="Item 5" Height="100" x:Name="Item5"/>
<TextBox Text="Item 6" Height="100" x:Name="Item6"/>
</StackPanel>
</ScrollViewer>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Item6.Focus();
}
Thank you for your help.