1 year ago

#340116

test-img

Joe

WPF ListBox eats mouse wheel messages breaking my page scroll

I've got a page in a TabControl with a lot of content. I can scroll the whole page just with the mouse wheel almost anywhere on the page.

But this behavior breaks whenever when the mouse is over one ListBox on the page. Whenever I'm above that the wheel has no effect. It's annoying and I would like to fix it.

As a diagnostic, I tried adding handlers to the ListBox for PreviewMouseWheel and MouseWheel. I get the former, but never the latter. So clearly the ListBox is eating the wheel messages, preventing them from escaping.

I need to let those wheel messages escape to the containing page so that it will scroll normally. But at the same time I still need it to be able handle mouse clicks and selection so I cannot change IsHitTestVisible or anything like that. (I am using a ListBox because I want multiple selection)

(If this were a mouse button handler, I would try to override the virtual OnMouseLeftButton down function, call the base class and then re-set the Handled property of the event handler. But there are no such corresponding virtual functions for wheel events)

Is there some property I can set to turn off this behavior? Maybe some event handler I can write? I feel like this should be easy but I cannot figure it out.

I don't think it matters but here is the XAML of the ListBox on the page

<ListBox Margin="20, 5" DockPanel.Dock="Top" SelectionMode="Multiple"
    ItemsSource="{Binding ResettableSettings}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type svc:ISettingsGroup}">
            <StackPanel>
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="10"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="10"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" 
                        Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"/>

                    <TextBlock Text="{x:Static res:ConfigureStrings.Settings}" 
                        Grid.Row="2" Grid.Column="0"/>
                    <TextBlock Text="{Binding Description}"
                        Grid.Row="2" Grid.Column="2"/>
                    <TextBlock Text="Effects"
                        Grid.Row="4" Grid.Column="0"/>
                    <TextBlock Text="{Binding ResetEffects}"
                        Grid.Row="4" Grid.Column="2"/>
                 </Grid>
           </StackPanel>
        </DataTemplate>

    </ListBox.ItemTemplate>
</ListBox>

wpf

listbox

mousewheel

0 Answers

Your Answer

Accepted video resources