2

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 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?

1 Answer 1

3

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

3
  • Thanks, but as mentioned in my question, this solution does not work--scrolling over the empty space still does nothing.
    – Matthew
    Jul 25, 2011 at 20:31
  • @Matthew How did you test it earlier? If you used a border, did you specify a background color? I believe the default background is transparent, which lets events pass through the control.
    – Rachel
    Jul 25, 2011 at 21:45
  • Ah, I just had to set the background. Setting it to Transparent is fine, but not setting it lets events pass through the control. Thanks!
    – Matthew
    Jul 25, 2011 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.