jeudi 20 octobre 2016

Set Focus to Checkbox in ComboBox in order to get keyboard control

I have a ComboBox which has CheckBoxes as items:

<ComboBox ItemsSource="{Binding SomeCollectionProperty}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding SomeBoolProperty}" Width="20" />
                <TextBlock Text="{Binding Name}" Width="140" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

If the ComboBox gets keyboard focus, I can select an item with the up and down arrow keys. I can then switch focus to the CheckBox with tab and am able to switch the CheckBox's IsChecked state by pressing space.
I'm looking for a solution that allows me to do the same but without manually switching focus to the CheckBox. My desired behavior would be:

  1. Choose item with arrow keys
  2. Choose state with space

I tried to do it with EventTrigger and InputBinding and by fiddling around with the Focusable property but with no luck.
I would prefer a solution in xaml but a code-behind solution would still be appreciated.




Aucun commentaire:

Enregistrer un commentaire