在WPF列表视图中逐项滚动

本文关键字:滚动 视图 WPF 列表 | 更新日期: 2023-09-27 17:58:55

我有一个高度很小的列表视图,但有3-4个列表视图项,占据了列表视图的整个大小(因此一次只能显示一个项)

如果用户在上面滚动,列表视图不会一次滚动1个项目,而是一次滚动2个项目(单次滚动)

你如何设置为1滚动=向下/向上一项?

希望我能澄清这一点,如果不是告诉我的话。

在WPF列表视图中逐项滚动

我想您在这里谈论的是鼠标滚轮滚动。

鼠标滚轮滚动实际上取决于IScrollInfo实现。我建议您在ScrollViewer之前自己处理鼠标滚轮事件。所以基本上,你可以做如下的事情:

处理ListBox 上的PreviewMouseWheel事件

<ListBox PreviewMouseWheel="ListBox_PreviewMouseWheel" Height="108" Width="100" x:Name="list" >
    <Button Content="Button 1" Height="100"/>
    <Button Content="Button 2" Height="100"/>
    <Button Content="Button 3" Height="100"/>
    <Button Content="Button 4" Height="100"/>
    <Button Content="Button 5" Height="100"/>
    <Button Content="Button 6" Height="100"/>
    <Button Content="Button 7" Height="100"/>
    <Button Content="Button 8" Height="100"/>
    <Button Content="Button 9" Height="100"/>
</ListBox>

在后面的代码中,当向下或向上滚动时激发ScrollBar.LineDownCommandScrollBar.LineUpCommand

private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
    {
        ScrollBar.LineDownCommand.Execute(null, e.OriginalSource as IInputElement);
    }
    if (e.Delta < 0)
    {
        ScrollBar.LineUpCommand.Execute(null, e.OriginalSource as IInputElement);
    }
    e.Handled = true;
}

因此,您将鼠标滚轮滚动到LineDown/LineUp