1

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.

Vladimir Amiorkov
  • 2,652
  • 3
  • 17
  • 35

1 Answers1

0

After a deeper searching trough the internet I found this answer. Thanks to AndrewS for his answer in that thread. Basically what I had to do is handle the RequestBringIntoView event of the Panel inside the ScrollViewer control or in my example the StackPanel.

Community
  • 1
  • 1
Vladimir Amiorkov
  • 2,652
  • 3
  • 17
  • 35
  • can you enlighten me on how did you handle the RequestBringtIntoVIew of the StackPanel by code? I'm still confused. – Red David Apr 19 '18 at 16:03
  • 1
    It is an event, simply attach an event handler https://msdn.microsoft.com/en-us/library/system.windows.requestbringintovieweventhandler(v=vs.110).aspx – Vladimir Amiorkov Apr 19 '18 at 19:38