I have a ListBox with a TextBox above it. I would like to use the arrow keys to navigate from the ListBox to the TextBox.
The intention is that if the first item in the ListBox is selected, and the user keys up, the TextBox will get focus.
I nearly have this working, but the problem is that when the user keys up, the SelectedItem is changed before the KeyUp event is raised. This means that the navigation to the TextBox happens when the user has selected the second item in the ListBox.
How can I trap the keyup event on the first item in a ListBox?
<StackPanel>
<TextBox Name="TextBox1"></TextBox>
<ListBox Name="ListBox1" KeyUp="ListBox_KeyUp">
<ListBoxItem>a</ListBoxItem>
<ListBoxItem>b</ListBoxItem>
<ListBoxItem>c</ListBoxItem>
</ListBox>
</StackPanel>
private void ListBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up)
{
if (this.ListBox1.SelectedIndex == 0)
this.TextBox1.Focus();
}
}