0

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.

Pawcio
  • 399
  • 5
  • 15
  • for that kind of job I'd advise you to use binding, have you an idea already of what it is? – Siegfried.V Dec 04 '18 at 14:52
  • I do but the actual string that where i'm actually matching searching input character is nested deep inside data object, and I don't know how I could use binding to achieve my goal. – Pawcio Dec 04 '18 at 15:04
  • I really don't understand what you're trying to do – Siegfried.V Dec 05 '18 at 06:42
  • I wrote some more details – Pawcio Dec 05 '18 at 09:58
  • You can try selecting the item with enter in code. E.g. listen to "Enter" keydown, when it happens, set "SelectedItem" to the one which is highlighted. Maybe using a "IsHighlighted" property (if it exists) or using some other way that will tell you which item is supposed to be selected. Can't write code atm, so just throwing ideas – MaxB Dec 05 '18 at 14:02
  • I probably could to this but it's not easy to tell how exactly I should handle the keyboard events to not block something else - I would need this only when CB is expanded and how know for what other things the Enter would be used on the control so this makes things complicated and I would rather not try to touch that for now as this seem to be minor issue for me and I could introduce a serious one with keyboard events... – Pawcio Dec 05 '18 at 15:52
  • and still, I think the real question would be why Keyboard focus in not working. – Pawcio Dec 05 '18 at 15:55

0 Answers0