background
I have custom control, containing a Popup
which contains a ListBox
Requirement
I need to close the popup and do some logic with the selected item when the user chooses an item. User chooses an item when :
- he click's on the item with the mouse.
- he selected and item through keyboard navigation (up/down keys) and clicks enter
Problem
I have implemented all the above, but my problem is with the events to listen to inorder to execute my logic.
If I execute my logic on the SelectionChanged
event, It will not fire when the user clicks on the selected item, so I'm missing my first scenario.
If I execute my logic on the PreviewMouseLeftButtonDown
It will fire before the selection changed so I don't know what the user has chosen. This is also why I can't use both.
I thought of listening on the ListBoxItem
events to do this (How to capture a mouse click on an Item in a ListBox in WPF?) or firing a command from an implicit ListBoxItem
style (WPF Interaction triggers in a Style to invoke commands on View Model) but they didn't work for me.
The best idea that I came up with, is to create some kind of a "post selection" MouseButtonDown event via behaviours or actions, but I am not sure how, or if this is even the way to go.
Any Idea how to create such a thing? Or is there a better solution for this?