在WPF列表视图中逐项滚动

时间:2020-12-22 14:30:05

I have a listview which is quite small in height, but has 3-4 listviewitems which takes up the whole size of the Listview (so only one item can be displayed at once)

我有一个列表视图,其高度非常小,但有3-4个listviewitems,它占用了Listview的整个大小(所以一次只能显示一个项目)

And if the user scrolls on it, the listview doesn't scroll 1 item at a time, it scrolls 2 items at a time (single scroll)

如果用户滚动它,列表视图不会一次滚动1个项目,它一次滚动2个项目(单个滚动)

How would you set it so 1 scroll = one item down/up?

你如何设置它如此滚动=一个项目向下/向上?

Hope I made myself clear with this, if not just tell me.

希望我明白这一点,如果不是只是告诉我。

1 个解决方案

#1


3  

I assume you're talking about the MouseWheel scroll here.

我假设你在这里谈论MouseWheel滚动。

The MouseWheel scroll really depends on the IScrollInfo implementation. I suggest you to handle the MouseWheel event yourself before the ScrollViewer does. So basically, you could do something like following:

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

Handle the PreviewMouseWheel event on ListBox

处理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>

In the code behind, fire the ScrollBar.LineDownCommand or ScrollBar.LineUpCommand when you scroll down or up.

在后面的代码中,向下或向上滚动时触发ScrollBar.LineDownCommand或ScrollBar.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;
}

Therefore, you turned the MouseWheel scroll into the LineDown/LineUp.

因此,您将MouseWheel滚动转换为LineDown / LineUp。

#1


3  

I assume you're talking about the MouseWheel scroll here.

我假设你在这里谈论MouseWheel滚动。

The MouseWheel scroll really depends on the IScrollInfo implementation. I suggest you to handle the MouseWheel event yourself before the ScrollViewer does. So basically, you could do something like following:

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

Handle the PreviewMouseWheel event on ListBox

处理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>

In the code behind, fire the ScrollBar.LineDownCommand or ScrollBar.LineUpCommand when you scroll down or up.

在后面的代码中,向下或向上滚动时触发ScrollBar.LineDownCommand或ScrollBar.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;
}

Therefore, you turned the MouseWheel scroll into the LineDown/LineUp.

因此,您将MouseWheel滚动转换为LineDown / LineUp。