简体   繁体   中英

how can I scroll listbox inside scrollviewer using mouse wheel

xaml:

    <ScrollViewer HorizontalAlignment="Center" PreviewMouseWheel="myScrollViewer_PreviewMouseWheel"  Name="myScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto">
            <Grid  Width="1200">
                <ListBox ScrollViewer.VerticalScrollBarVisibility="Visible" MouseMove="listbox_MouseMove" Background="AliceBlue" Height="350" Width="200" HorizontalAlignment="Center" Name="listbox"/>
            </Grid>
    </ScrollViewer>

cs:

    private void myScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta < 0) // wheel down
        {
            if (myScrollViewer.HorizontalOffset + e.Delta > 0)
            {
                myScrollViewer.ScrollToHorizontalOffset(myScrollViewer.HorizontalOffset + e.Delta);
            }
            else
            {
                myScrollViewer.ScrollToLeftEnd();
            }
        }
        else //wheel up
        {
            if (myScrollViewer.ExtentWidth > myScrollViewer.HorizontalOffset + e.Delta)
            {
                myScrollViewer.ScrollToHorizontalOffset(myScrollViewer.HorizontalOffset + e.Delta);
            }
            else
            {
                myScrollViewer.ScrollToRightEnd();
            }
        }
    }

Now on mouse wheel event scrollviewer is scrolling and if my mouse goes over ListBox. I want: if my mouse goes over ListBox and stays a while (maybe 1 or 2 seconds) scrolling should move to ListBox. If mouse does not stay over ListBox a while (1 or 2 seconds) scrolling should not move to ListBox. There might be multi ListBoxes in ScrollViewer.

Add that to your listbox:

ScrollViewer.CanContentScroll="True"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM