views:

646

answers:

2

I have a custom ListBox which uses a custom Panel as ItemsHost. I want to have control over mouse wheel input, so that turning the wheel changes the single selected item.

I believe that the best method to do so is to handle the OnPreviewMouseWheel event (although this is only have what I want since it doesn't provide horizontal wheel data).

Now the big question: Is there a best practice where to handle OnPreviewMouseWheel? In ListBox (which by default doesn't have a clue about the arrangement of the Panel's child elements) or in Panel (which by default doesn't have a clue about the "IsSelected" property of it's child elements)?

A: 

I think you should do this from the ListBox. ListBox inherits from Selector which handles everything to do with selection.

The mousewheel selection behavior could apply with any kind of Panel (it might even be a good idea to implement it using a standard ListBox first), and you might want to use the Panel somewhere outside of a ListBox where the selection logic would make no sense.

Robert Macnee
A: 

It should be done from the ListBox as only it knows about what item is currently selected. The panel doesn't want or need to know if it selected or not.

I would recommend implementing this as an attached behavior so you can re-use the functionality multiple times.

To do this:
- create a new class (maybe called ListBoxSelector) with an attached property called MouseWheelChangesSelection (true/false).
- add a PropertyNotifyChangedEvent and when the property is changed register an event listener for the PreviewMouseWheel / MouseWheel events.

You could change the currently selected item by either:
- incrementing the selected index; or
- using the collection view source's move prev/next

Dennis Roche