I'm using this method to focus on specific combo box item.
/// <summary>Combo box focus index.</summary>
private int cbfi = -1;
/// <summary>Focus specified list item based on char match to language name.</summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void onLangComboInput(object sender, TextCompositionEventArgs e) {
var cb = sender as ComboBox;
var ch = e.Text;
var it = cb.findNext<StackPanel, UIElement>(
//...
);
if (it == null) return;
it.Focusable = true;
it.Focus();
Keyboard.Focus(it);
}
The method works in sense that it focuses correctly item from the list, and the list is scrolled fine but I have a problem when applying focused item as ComboBox SelectedItem
when I Hit Enter - some other item than the focused one is selected and I can't figure out why. It looks like keyboard focus is not set correctly.
Update
Basically I have regular System.Windows.Controls.Combobox
with list of about 300 custom view items.
I want the basic functionality to jump to item which starts with corresponding character in expanded list when I hit the character on keyboard (But I don't want automatic selection, just focus and scroll to that element).
And again. I have CUSTOM item view which I add to ListView
through Items.Add(mycustomUIElement)
and the ListView
instance is set as DataContext
of the ComboBox
. Each view item have it's own DataContext
assigned to particular data item. And the data item contains nested string property that is compared with input character. Also I have alternative string if the primary on is not available so I can't really use build in functionality like in this question.
The item searching and scrolling is working fine but but keyboard focusing seem to not always work. Like when I hit enter, wrong item is selected, or when use arrow keys, next item is not the neighbor of the target items.