There is a markup:

<ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="400" Style="{DynamicResource ScrollViewerDefaultStyle}"> <ItemsControl x:Name="_allTags" ItemsSource="{Binding}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Width="{Binding Path=ActualWidth, ElementName=SearchTextBox}" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type s:String}"> <Button Template="{StaticResource ItemTemplate}" PreviewKeyDown="PreviewHashTag_KeyDown" Click="Item_Click"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> 

There is a code that, when you press Down in the text field (the code does not bring it) transfers the focus to the ItemsControl, where you can then freely move between items using the arrows. But it was so until I registered the style: ScrollViewerDefaultStyle

Style:

 <Style x:Key="ScrollViewerDefaultStyle" TargetType="{x:Type ScrollViewer}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ScrollViewer}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ScrollContentPresenter KeyboardNavigation.DirectionalNavigation="Cycle" Grid.ColumnSpan="2" Grid.RowSpan="2" CanContentScroll="{TemplateBinding CanContentScroll}" Margin="{TemplateBinding Padding}"/> <ScrollBar x:Name="PART_VerticalScrollBar" Style="{StaticResource ScrollBarStyle}" Orientation="Vertical" Grid.Column="1" Value="{TemplateBinding VerticalOffset}" Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/> <ScrollBar x:Name="PART_HorizontalScrollBar" Style="{StaticResource ScrollBarStyle}" Orientation="Horizontal" Grid.Row="1" Value="{TemplateBinding HorizontalOffset}" Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> 

After I applied the style, when I press Down, the focus is transferred, but the navigation is not on items, but on the whole ScrollViewer, i.e. there is a simple scrolling. I can not understand what is responsible for this.

It was played with KeyboardNavigation.DirectionalNavigation, but it led nowhere. Help me to understand.

  • There is no standard style at all. KeyboardNavigation.DirectionalNavigation - Andrey NOP
  • In general, you use the wrong approach, in order to scroll elements inside the ItemsControl, you need the ScrollViewer to be inside it, not outside. Look at this question: ru.stackoverflow.com/q/790433/218063 - Andrey NOP
  • @AndreyNOP Transferred ScrollViewer to ItemsControl, now when you press Down you can see that focus comes to the first item as before, but subsequent pressing of the arrows leads to a simple scrolling - Sergey
  • By the way, why do you think that the transition between controls should be performed by pressing the "Down" key? Standard way to switch between controls - Tab key - Andrey NOP
  • @AndrewNOP I base on what happened with the default behavior, before I applied the style. It was enough for me to simply transfer the focus to ItemsControl and just move the arrows, both up and down and left-right - Sergey

1 answer 1

In the standard ScrollViewer template, the ScrollViewer area has the name PART_ScrollContentPresenter , you didn’t designate this area in your template, so you now have the content separately, and the scroll area separately. Just assign the appropriate name:

 <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" ... 

ScrollViewer element styles and templates

  • Exactly, thanks! And why is the name? Is it somewhere in the guts used? - Sergey
  • Exactly! The parts of the template inside the control that are responsible for the functionality are searched by name. If there is no name, then the control does not support this part of the functional. - Andrey NOP
  • Thank you very much. Now when redefining templates I will take this into account. - Sergey