0
votes

Just drop a button and a combobox inside a new WPF application

On the button click simply set the focus to the combobox with a comboBox1.Focus() (or anything else possible)

questions :

1- When setting focus from code (with focus()) the combobox never receive the focus. why?

2- When navigating with the Tab key the combobox receive the focus and display a kind of focus selector around the control? is there any way to do the same (a real focus) from code?

Thanks

4
What's the code you have so far ? - Gabriel L.
nothing just comboBox1.SetFocus() with a standard combobox with default properties (focusable of course) - michelqa
but I also tried Keyboard.FocusManager.SetFocusedElement , Keyboard.Focus(element), getting focus to the textbox inside, routed event with mousedown etc..etc.. - michelqa

4 Answers

0
votes

I think it is actually working - the problem is that in non-editable ComboBox it is hard to tell visually if the focus is there.

If you add IsEditable="True" you will see it the cursor will move to the ComboBox when you click the button.

The focus selector you see with the Tab key is keyboard focus. Try this: on your app, without making ComboBox editable, navigate to the button and press SPACE. You will see the rectangle selection appear around the ComboBox, because the keyboard focus will move there.

0
votes

Focus style (dotted line ) is only appeared when we used Tab key ..thats why combobox is only taken focus not focus style on button click..but you can show focus by opening dropdown like below

     private void Button_Click_1(object sender, RoutedEventArgs e)
    {                              
         Cmb.Focus();
         Cmb.IsDropDownOpen = true;                                
    }

Sorry if I am wrong to your approach.

0
votes

Read about selectionboxitem of the combobox which is responsible for selection in comobox edit area. Try setting the IsSelected along with the Focus property.

There are lots of new things on focus handling as compared to Winforms where it was just select and focus. Check the API:

http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox%28v=vs.110%29.aspx

0
votes

If you have restyled the combobox, be careful with "OverridesDefaultStyle". Setting this to true can hide the dotted focus rectangle.