How I can get Selected Item when I navigate trough the List via Keyboard?
It looks like MHE is selected but no Property of the XamComboEditor is filled with that Item
So in my Usecase I registered the PreviewKeyDown Event, so when I hit Enter I want to have the Selected Item which is selected by Keyboard.
The Problem is that in the PreviewKeyDown Event it's not possible for me to find out which item was now selected via KeyUp and KeyDown.
I have to know it in PreviewKeyDown because I have to work with that item in that Event Handler. Is there a chance to get it? So The SelectionChangedEvent is fired after KeyDown and then its too late for me (so the SelectedItem is set also then)
CustomValueEnteredAction = CustomValueEnteredActions.Allow;
Hello CSchmitt,
Thank you for the screenshots you have provided.
The blue highlight rectangle around the ComboEditorItemControl from your first image indicates the currently active/focused element (the one that will get selected if Enter is pressed) and not the selected one.
In order to get the underlying data item of the currently active combo item inside the PreviewKeyDown event of the XamComboEditor prior to the SelectionChanged event, I can suggest you go down through the visual tree of the editor's dropdown and get the respective ComboEditorItemControl whose focus rectangle is currently visible.
Code-behind:
private void combo_PreviewKeyDown(object sender, KeyEventArgs e){ var combo = (sender as XamComboEditor); if (combo.IsDropDownOpen && e.Key == Key.Enter) { var item = GetActiveItem(combo); MessageBox.Show("Active item: " + item); }} private static object GetActiveItem(XamComboEditor combo){ var dropdownPopup = GetOpenPopups().FirstOrDefault().Child; if (dropdownPopup == null) return null; var itemsPanel = Utilities.GetDescendantFromName(dropdownPopup, "ItemsPanel") as ItemsPanel; if (itemsPanel == null) return null; // Get the ComboEditorItemControl whose inner focus rectangle is visible. // (the blue highlight around the item indicates it is visible) // var activeComboEditorItemControl = itemsPanel.Children .OfType<ComboEditorItemControl>() .FirstOrDefault(it => (Utilities.GetDescendantFromName(it, "FocusVisualElement") as Rectangle).IsVisible); var item = activeComboEditorItemControl.Item.Data; return item;}
private static object GetActiveItem(XamComboEditor combo){ var dropdownPopup = GetOpenPopups().FirstOrDefault().Child; if (dropdownPopup == null) return null;
var itemsPanel = Utilities.GetDescendantFromName(dropdownPopup, "ItemsPanel") as ItemsPanel; if (itemsPanel == null) return null;
// Get the ComboEditorItemControl whose inner focus rectangle is visible. // (the blue highlight around the item indicates it is visible) // var activeComboEditorItemControl = itemsPanel.Children .OfType<ComboEditorItemControl>() .FirstOrDefault(it => (Utilities.GetDescendantFromName(it, "FocusVisualElement") as Rectangle).IsVisible); var item = activeComboEditorItemControl.Item.Data; return item;}
I have attached a sample application that demonstrates the approach from above.
If you have any questions, please let me know.
Thank you, it works in your example when you use strings but not if you have a class behind like this
public ObservableCollection<Test> Items { get; set; }
public class Test{public string Bla { get; }
public Test(string bla){Bla = bla;}}
public MainWindow(){InitializeComponent();this.DataContext = this;
Items = new ObservableCollection<Test>(){new Test("tem01"), new Test("Item02"), new Test("tem03"), new Test("Item04"), new Test("Item05")};
OK I can solve it with .FirstOrDefault(it => it.Content != null && . Then its working
Hello,
Thank you for your feedback.
I am glad to know that you were able to achieve the functionality you were looking for. I believe this thread can help other people looking for a similar solution.