1

I have a WPF checkbox list containing car models. When I tab into the list the follwoing happens:

1) On first tab key press, the list item is selected

2) On second tab key press, the actual checkbox is selected allowing me to set the IsChecked state using the spacebar key.

Is it possible to automatically move the cursor to the checkbox when the list item is selected? (trying to avoid hitting tab twice)

XAML:

<ListBox ItemsSource="{Binding Cars}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding Make}" IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120

2 Answers2

1

Try adding this to your ListBox:

        <ListBox.ItemContainerStyle>
          <Style TargetType="Control">
            <Setter Property="Focusable" Value="False" />
          </Style>
        </ListBox.ItemContainerStyle>
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
1

Tried Dan's solution, it works but it moves the focus out of the ListBox control after selecting the first checkbox, I was looking for something which prevents the focus from being set to the selected listbox item while still allowing to tab through all the checkboxes in the list.

The following link helped me - Keyboard focus to list box items in WPF

I've made a few changes to have create border as well as the verticall scrollbar, complete XAML below:

<Border BorderThickness="0.8" BorderBrush="Gray">
        <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="1">
            <ItemsControl ItemsSource="{Binding Cars}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Make}" IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Border>
Community
  • 1
  • 1
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120