michelqa
michelqa

Reputation: 157

WPF setting focus to Combobox

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

Upvotes: 1

Views: 13625

Answers (4)

Anthony Hayward
Anthony Hayward

Reputation: 2352

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

Upvotes: 0

sunnytyra
sunnytyra

Reputation: 89

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

Upvotes: 0

Heena
Heena

Reputation: 8654

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.

Upvotes: 2

FarmerBob
FarmerBob

Reputation: 1364

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.

Upvotes: 0

Related Questions