如何使用鼠标滚轮在scrollviewer中滚动列表框

时间:2015-02-23 08:52:15

标签: c# wpf xaml scrollviewer mousewheel

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();
            }
        }
    }

现在,鼠标滚轮事件滚动查看器正在滚动,如果我的鼠标移过ListBox。我想:如果我的鼠标越过ListBox并保持一段时间(可能是1或2秒),滚动应该移动到ListBox。如果鼠标没有停留在ListBox上一段时间(1或2秒),则滚动不应移动到ListBox。 ScrollViewer中可能有多个ListBox。

1 个答案:

答案 0 :(得分:0)

将其添加到您的列表框中:

ScrollViewer.CanContentScroll = “真”