I am writing an application with a listbox allowing multi-selection (SelectionMode=Multiple); the items in the lisbox are ingredients for a recipe.
Unfortunately, clicking on a listbox item selects this item, which may not be desired. I would like the following scenario:
- the user clicks on the listbox in order to select the listbox (the listbox itself, not an item)
- the user scrolls to the right item and selects it
What I did is style the ListBoxItem to include a checkbox and a ContentPresenter (like in this blog). Still, clicking on the ingredient name selects it. So, I trap the MouseDown event on the textblock containing the ingredient name, find the underlying ListBoxItem, call Focus() on it and set the Handled property of the event to true.
Now, the Listbox item has the focus but is not selected. Using the up and down keys shows that the focus was on the right item. My problem is that the user cannot see that he has clicked on the right item. The dotted rectangle is not shown on this item. Here is the result:
And here is what I'd like:
I've tried calling private WPF methods, like KeyboardNavigation.ShowFocusVisual, I've tried sending keystrokes to the listbox (when done by a human, pressing the right cursor key or the Alt key makes the dotted rectangle appear).
Any idea ?