UWP - Prevent default ItemsControl focus handling

264 views Asked by At

I have an ItemsControl with IsTabStop="False". When I click below the last item in the ItemsControl, the first item in the ItemsControl is focused.

I want to set focus to the last item in the list instead. With the following code, focus is set to the last child of the ItemsControl... until I release the mouse at which point the first item becomes focused. Which event/override do I need to handle instead?

protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
  e.Handled = true;
  var item = VisualTreeHelper.GetChild(Items.ItemsPanelRoot.Children.Last() as DependencyObject, 0) as ContentControl;
  (item.Content as Control).Focus(FocusState.Keyboard);
}

UPDATE

Here's some sample code that illustrates the problem.

<ScrollViewer IsTabStop="False">
  <ItemsControl IsTabStop="False">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <TextBox Text="{Binding}" HorizontalAlignment="Stretch" />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.Items>
      <x:Int32>1</x:Int32>
      <x:Int32>2</x:Int32>
      <x:Int32>3</x:Int32>
    </ItemsControl.Items>
  </ItemsControl>
</ScrollViewer>

When I click empty space below textbox 3, textbox 1 is focused. I want to focus textbox 3 instead.

enter image description here

0

There are 0 answers