0

I am facing the following issue. When i am removing the first element from a list box which has keyboard focus(by refreshing the item source) the keyboard focus is moving to the parent window. I want to retain the keyboard focus on the fist listbox item. So i came up with the following code

<Grid.Resources>
<Style TargetType="{x:Type ListBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding FirstRowDeleted}" Value="True">
            <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=MyListBox}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Using this code i am able to move the focus to the list box itself. How can i move the focus to the first listbox item? MyListBox[0] does not seem to work. Any suggestions?

Thanking you Kaddy

H.B.
  • 166,899
  • 29
  • 327
  • 400
Kaddy
  • 1,226
  • 7
  • 21
  • 34

2 Answers2

0

I found answer here: How do you programmatically set focus to the SelectedItem in a WPF ListBox that already has focus?

var listBoxItem = (ListBoxItem) MainListBox
    .ItemContainerGenerator
    .ContainerFromItem(MainListBox.SelectedItem);
listBoxItem.Focus();

Thanks for @Jeff for the answer.

Tamás Deák
  • 175
  • 1
  • 13
-3

This code applies the focus function to the first input text when the page loads. try it out

$(document).ready(function () {

                $("#input:text:visible:first").focus();


            });