I have a ListBox with ItemsTemplate set.
Now I want to catch Ctrl + Left click on a ListBoxItem.
I found KeyBoard class that should give me modifier keys. Now how do I get the click event on the ListBoxItem? Even better, how do I bind it to ICommand.
I found some bits and pieces but don’t know how to connect them. It seems InputBinding seems could help me or EventSetter.
Below is a simple example that handles Ctrl + PreviewMouseLeftButtonDown using an EventSetter in the ListBoxItem’s Style. This is probably what you want.
XAML:
Code-Behind:
To bind it to an ICommand, you can use an attached behavior like the EventToCommand behavior discussed here.
EDIT:
To address your comment, handling the Click-event will be a bit tricky for the ListBoxItem because of two things: 1) The ListBoxItem doesn’t have a click event, and 2) The ListBoxItem internally handles some of the MouseEvents. Anyway, I came up with a simulated, attached ClickEvent to make it work. See below. Hope it works.
Sample usage is shown below. I can’t seem to set the attached event in XAML (I’m not sure why) so I had to do a workaround here. What I do is wait ’til the ListBoxItem gets loaded and Attach the event handler in the code-behind.
XAML:
Code-Behind: