2

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

Relativity
  • 6,690
  • 22
  • 78
  • 128
  • You are currently using a combobox wpf classic? or any third party library (eg telerik)? – Mate Oct 23 '12 at 19:36

3 Answers3

3

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;
        }
Community
  • 1
  • 1
Mate
  • 4,976
  • 2
  • 32
  • 38
  • setting IsTabstop will never work. I have tried that. I tried all kind of keyboard navitaions..but of no luck – Relativity Oct 23 '12 at 19:32
  • Please try to use the last option. I understand that you have a headache because this piece of code ... but if you google you will find it is a known bug. Also I hope you find a better solution. – Mate Oct 23 '12 at 20:44
  • I tried this...my dropdown is getting closed...but the focus is not moving to next control. – Relativity Oct 23 '12 at 21:14
0

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.

aa aa
  • 399
  • 3
  • 6
0

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" />