2
votes

I have a WPF combobox...which is non-editable. When I tab into this combobox...I have a style setter (<Setter Property="IsDropDownOpen" Value="True"/>) to open the combobox. But when I tab again..the focus move to next item in the opened combobox....and it cycles over there. I am not able to tab out to next control.

What is wrong here?

Thanks

3
You are currently using a combobox wpf classic? or any third party library (eg telerik)? - Mate

3 Answers

2
votes

Try :

<Style TargetType="{x:Type ComboBoxItem}">
    <Setter Property="IsTabStop" Value="False"/>
</Style>

Or

Work with KeyboardNavigation :

WPF tab order with custom controls?

Not recommend, but works...

      <Grid>
            <ComboBox Grid.Row="1" Margin="0,0,0,0" Name="comboBox1" HorizontalAlignment="Left" Width="120" Height="20"  IsEditable="False" KeyDown="comboBox1_KeyDown"  GotKeyboardFocus="comboBox1_GotKeyboardFocus" >
                <ComboBox.Style>
                    <Style TargetType="{x:Type ComboBox}">
                    <Style.Triggers>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="IsDropDownOpen" Value="True" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
                </ComboBox.Style>
                <ComboBoxItem>Male</ComboBoxItem>
                <ComboBoxItem>Female</ComboBoxItem>
                <ComboBoxItem>Unknown</ComboBoxItem>
            </ComboBox>

        </Grid>

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        ComboBox cb = sender as ComboBox;
        if (e.Key == Key.Tab && cb.IsDropDownOpen)
        {
            ComboBoxItem item = FocusManager.GetFocusedElement(Window.GetWindow(this)) as ComboBoxItem;
            cb.SelectedItem = item;
            cb.IsDropDownOpen = false;
            e.Handled = true;
        }
    }

   private void comboBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            cb.IsDropDownOpen = true;
        }
0
votes

You can achieve the same by the following code:-

 private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  {
    if (e.Key == Key.Enter)
        {
            IsDropDownOpen = true;
            e.Handled = true;
        }
  }

Now when focus will set on ComboBox then you need to hit enter to open drop down and you can use down key word to traverse ComboBox items. To move to the next control,you need to press tab.

0
votes

I had the same problem, solved it like this in XAML:

<Style x:Key="RadComboBoxItemStyle" TargetType="telerik:RadComboBoxItem">
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="IsHitTestVisible"
                Value="True" />