tags:

views:

124

answers:

2

I have this ListBox:

<ListBox Name="lbColor">
    <ListBoxItem Content="Blue"/>
    <ListBoxItem Content="Red"/>
    <ListBoxItem Content="Orange"/>
</ListBox>

This code pre-selects the choice alright, but doesn't set the focus, how can I do that?

public Window1()
{
    InitializeComponent();

    lbColor.SelectedIndex = 1;
    lbColor.Focus = 1;
}
+3  A: 

You can use the Focus method:

public Window1()
{
    InitializeComponent();
    lbColor.SelectedIndex = 1;
    lbColor.Focus();
}
Darin Dimitrov
+1  A: 

I think, that you have to inherit from UIElement-Class and set true to UIElement.IsFocusable. Now you should be able to set the focus to the listbox with lblcolor.Focus()! I hope that this will help.

regards, cordell

cordellcp3