How can I listen for mouse wheel events in the Margin between two items in an ItemsControl?
I have an ItemsControl
that looks something like this:
<ItemsControl ItemsSource="{Binding}" PreviewMouseWheel="ItemsControl_PreviewMouseWheel" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,4"><!--Margin to keep the items from being smashed too closely together-->
...
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The ItemsControl
will be one of several items in a scrollable area, but if开发者_StackOverflow社区 I use the mouse wheel over the ItemsControl
, nothing happens. So I want to forward the mouse wheel events further up the tree:
private void ItemsControl_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent
});
}
This mostly works. But when the mouse scrolls over the Margin
area (specified by Margin="0,4"
) between two items in the control, nothing happens. I tried putting the grid inside a decorator, such as a Border
, but that doesn't seem to help. How can I capture these mouse wheel events, and forward them up the tree?
The Margin is the area of space left around a Control, and since it's just empty space it doesn't process any events.
An alternative is to either nest controls, such as putting your Grid with the Margin inside a DockPanel without a Margin, or to use a WPF control that has the Padding
property such as Border
精彩评论